使用 apache PDF-Box 插入 PDF 附件的缩略图 [英] Insert a thumbnail for PDF attachment using apache PDF-Box
本文介绍了使用 apache PDF-Box 插入 PDF 附件的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个代码可以将文件附加到 PDF 文件.
I have a code to attach a file to a PDF file.
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
// read attachment file
File file = new File("/Users/TMac/Projects/Web/dir/index.html");
FileInputStream inputStream = new FileInputStream(file);
PDEmbeddedFile pdEmbeddedFile = new PDEmbeddedFile(doc, inputStream );
pdEmbeddedFile.setSubtype( "application/octet-stream" );
PDComplexFileSpecification fs = new PDComplexFileSpecification();
fs.setEmbeddedFile( pdEmbeddedFile );
fs.setFile("index.html");
int offsetX = 20;
int offsetY = 600;
PDAnnotationFileAttachment txtLink = new PDAnnotationFileAttachment();
txtLink.setFile(fs);
// Set the rectangle containing the link
PDRectangle position = new PDRectangle();
position.setLowerLeftX(offsetX);
position.setLowerLeftY(offsetY);
position.setUpperRightX(offsetX + 20);
position.setUpperRightY(offsetY + 20);
txtLink.setRectangle(position);
page.getAnnotations().add(txtLink);
doc.save("/Users/TMac/Projects/PDF/outputFiles/testHTML.pdf");
doc.close();
问题是附件图标看起来像这样:
The problem is the attachment icon is looks like this:
我需要用自定义图像替换此图标.我找到了一些与文本链接相关的示例.当我单击该图像时,它应该打开该文件.附件代码(上面的代码)工作正常.如何将自定义图像添加为缩略图?
I need to replace this icon with custom image.I have found some examples related to text links. when i click on that image it should open the file. Attachment code (above code) working fine. How can i add a custom image as a thumbnail ?
推荐答案
您需要创建一个外观流:
You need to create an appearance stream:
PDAnnotationFileAttachment txtLink = new PDAnnotationFileAttachment();
txtLink.setFile(fs);
// Set the rectangle containing the link
int offsetX = 20;
int offsetY = 600;
PDRectangle position = new PDRectangle();
position.setLowerLeftX(offsetX);
position.setLowerLeftY(offsetY);
position.setUpperRightX(offsetX + 20);
position.setUpperRightY(offsetY + 20);
txtLink.setRectangle(position);
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
PDAppearanceStream appearanceStream = new PDAppearanceStream(doc);
appearanceStream.setResources(new PDResources());
PDRectangle bbox = new PDRectangle(txtLink.getRectangle().getWidth(), txtLink.getRectangle().getHeight());
appearanceStream.setBBox(bbox);
try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream))
{
PDImageXObject image = PDImageXObject.createFromFile("image.jpg", doc);
cs.drawImage(image, 0, 0);
}
appearanceDictionary.setNormalAppearance(appearanceStream);
txtLink.setAppearance(appearanceDictionary);
page.getAnnotations().add(txtLink);
这篇关于使用 apache PDF-Box 插入 PDF 附件的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文