如何将通过字符串解码创建的图像插入pdfbox中的pdf
How to insert a image created by decode of a string to a pdf in pdfbox
我正在尝试将图像(需要通过 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)
感谢 Tilman Hausherr。
这是最终代码(只是核心部分):
int pageNumber = j;
PDPage page = document.getPage(pageNumber);
PDResources resources = page.getResources();
byte[] ba = java.util.Base64.getDecoder().decode(base64str);
PDImageXObject sigimg = PDImageXObject.createFromByteArray(document,ba,"signature");
float imgW = sigimg.getWidth();
float imgH = sigimg.getHeight();
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND, true,true);
PDRectangle sigRect = field.getWidgets().get(0).getRectangle();
float fieldW = sigRect.getWidth();
float fieldH = sigRect.getHeight();
if (imgW > fieldW || imgH > fieldH){
if(imgW/fieldW > imgH/fieldH){
sigimg.setWidth(Math.round(fieldW));
sigimg.setHeight(Math.round(imgH/imgW*fieldW));
}
else{
sigimg.setWidth(Math.round(imgW/imgH*fieldH));
sigimg.setHeight(Math.round(fieldH));
}
}
contentStream.drawImage(sigimg,sigRect.getLowerLeftX(),sigRect.getLowerLeftY());
contentStream.close();
我正在尝试将图像(需要通过 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)
感谢 Tilman Hausherr。 这是最终代码(只是核心部分):
int pageNumber = j;
PDPage page = document.getPage(pageNumber);
PDResources resources = page.getResources();
byte[] ba = java.util.Base64.getDecoder().decode(base64str);
PDImageXObject sigimg = PDImageXObject.createFromByteArray(document,ba,"signature");
float imgW = sigimg.getWidth();
float imgH = sigimg.getHeight();
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND, true,true);
PDRectangle sigRect = field.getWidgets().get(0).getRectangle();
float fieldW = sigRect.getWidth();
float fieldH = sigRect.getHeight();
if (imgW > fieldW || imgH > fieldH){
if(imgW/fieldW > imgH/fieldH){
sigimg.setWidth(Math.round(fieldW));
sigimg.setHeight(Math.round(imgH/imgW*fieldW));
}
else{
sigimg.setWidth(Math.round(imgW/imgH*fieldH));
sigimg.setHeight(Math.round(fieldH));
}
}
contentStream.drawImage(sigimg,sigRect.getLowerLeftX(),sigRect.getLowerLeftY());
contentStream.close();