[我正在尝试将图像(需要通过java.util.Base64.getDecoder()。decode(imageInputString)从字符串转换为图像)到pdf文件的特定位置。
代码的主要逻辑将是:
//create a PDImageXObject myImage first (or something that could be used in addImage method.
//And this is what I could not figure out how to accomplish.
//open the pdf file and use addImage to insert the image to the specific page at specific position.
PDDocument document = PDDocument.load(pdfFile);
PDPageContentStream contentStream = new PDPageContentStream(document, pageNumber);
contentStream.addImage(myImage,x,y);
document.save();
我发现的大多数教程都是通过读取图像文件来创建myImage的。有人可以帮我看看我是否可以做同样的事情,但是可以使用字节[],它是java.util.Base64.getDecoder()。decode(imageInputString)的输出?
谢谢!
您可以使用静态方法PDImageXObject.createFromByteArray()
,该方法根据内容检测文件类型,并确定哪种PDF图像类型/图像压缩最佳。 (javadoc)