PDFBox:从JAR资源加载图像到PDF中

huangapple go评论75阅读模式
英文:

PDFBox: Loading an Image Into PDF From a JAR Resource

问题

下午好。我有一个JAR文件,我已经将一些图片附加为资源放在一个名为logos的文件夹中。由于安全限制(我们不希望将图像文件暴露在与JAR相同的文件夹中),我被要求这样做。我最初尝试将这些图像加载为File对象,但显然这不起作用。我现在尝试使用InputStream将图像加载到所需的PDImageXObject中,但图像不会呈现在PDF中。以下是我正在使用的代码片段:

String logoName = "aLogoName.png";
PDDocument document = new PDDocument(); 

// 变量“generator”是用于生成PDF中操作的对象
InputStream logoFileAsStream = generator.getClass().getResourceAsStream("/" + logoName);
PDStream logoStream = new PDStream(document, logoFileAsStream);
PDImageXObject logoImage = new PDImageXObject(logoStream, new PDResources());

PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(logoImage, 500, 100);

请注意,我已经验证了资源的正确加载,因为使用logoFileAsStream.available()会返回不同的值,适用于不同的logo。在运行此代码后,图像实际上没有绘制在PDF上,在打开PDF时,会出现错误消息“此页面存在错误。Acrobat 可能无法正确显示页面。请联系创建PDF文档的人以纠正问题。”。是否有人可以帮助我找出代码片段有什么问题/从JAR中加载资源的不同解决方案?非常感谢。如果需要更多细节/澄清,请告诉我。

英文:

Good afternoon. I have a JAR file to which I have attached some images as resources in a folder called logos. I am being told to do this due to security restrictions (we don't want the image files to be exposed in the same folder as the JAR). I first tried to load these images in as if they were a File object, but that obviously doesn't work. I am now trying to use an InputStream to load the image into the required PDImageXObject, but the images are not rendering into the PDF. Here is a snippet of the code which I am using:

String logoName = "aLogoName.png";
PDDocument document = new PDDocument(); 

// the variable "generator" is an object used for operations in generating the PDF
InputStream logoFileAsStream = generator.getClass().getResourceAsStream("/" + logoName);
PDStream logoStream = new PDStream(document, logoFileAsStream);
PDImageXObject logoImage = new PDImageXObject(logoStream, new PDResources());

PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(logoImage, 500, 100);

Note that I have verified that the resource is getting loaded in correctly, as using logoFileAsStream.available() returns a different value for various logos. After running this code, the image does not actually get drawn on the PDF, and upon opening it, the error message "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem." appears. Could someone please help me figure what's wrong with that code snippet/a different solution to load my images in as a resource from the JAR? Thanks so much. Let me know if more details are needed/clarification.

答案1

得分: 2

这个PDImageXObject构造函数仅供PDFBox内部使用。您可以使用

PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(logoFileAsStream), logoName /* 可选,可以为null */)

以获得最大的灵活性,或者如果您知道它始终是一个PNG文件,

LosslessFactory.createFromImage(document, ImageIO.read(logoFileAsStream))

不要忘记关闭logoFileAsStream和contentStream。

英文:

This PDImageXObject constructor is for internal PDFBox use only. You can use

PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(logoFileAsStream), logoName /* optional, can be null */)

for maximum flexibility, or if you know it is always a PNG file

LosslessFactory.createFromImage(document, ImageIO.read(logoFileAsStream))

don't forget to close logoFileAsStream and contentStream.

huangapple
  • 本文由 发表于 2020年9月30日 02:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64125825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定