[英]Problem with empty page when using Apache PDFBox to add image to PDF
I am using this code: https://www.tutorialspoint.com/pdfbox/pdfbox_inserting_image.htm我正在使用此代码: https : //www.tutorialspoint.com/pdfbox/pdfbox_inserting_image.htm
To help me add an image to an existing PDF.帮助我将图像添加到现有 PDF。 The problem is that the file it creates is a blank page with only the image on it.
问题是它创建的文件是一个空白页,上面只有图像。
Here is my code:这是我的代码:
public void signPDF(PdfDTO pdfDTO) throws IOException{
//Loading an existing document
File file = new File(getAbsolutePdfPath(pdfDTO));
PDDocument doc = PDDocument.load(file);
//Retrieving the page
PDPage page = doc.getPage(0);
//a test to ensure the doc is loading correctly
PDDocument testDoc = new PDDocument();
testDoc.addPage(page);
testDoc.save("C:" + File.separator + "Users" + File.separator + "kdotson" + File.separator + "Documents" + File.separator + "test.pdf");
testDoc.close(); //this file is good so I know the doc is loading correctly
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile("C://test_images/signature.pdf", doc);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, page);
//Drawing the image in the PDF document
contents.drawImage(pdImage, 0, 0);
//Closing the PDPageContentStream object
contents.close();
//Saving the document
doc.save(new File(getSignedPdfLocation(pdfDTO))); //the created file has the image on it, so I know the image is loading correctly
//Closing the document
doc.close();
}
As far as I can tell, what I'm doing should work, and I don't get any errors, so what gives?据我所知,我正在做的事情应该可以工作,而且我没有收到任何错误,那么有什么用呢?
Please also have a look at the JavaDocs and sources of the library you try to work with.还请查看您尝试使用的库的 JavaDoc 和源代码。 You create a
PDPageContentStream
:您创建一个
PDPageContentStream
:
PDPageContentStream contents = new PDPageContentStream(doc, page);
This conductor is documented to overwrite all existing content streams of this page :该指挥被记录为覆盖此页面的所有现有内容流:
/**
* Create a new PDPage content stream. This constructor overwrites all existing content streams
* of this page.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException
Thus, you have to use a different constructor which keeps the current page contents, eg因此,您必须使用不同的构造函数来保留当前页面内容,例如
/**
* Create a new PDPage content stream.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @param appendContent Indicates whether content will be overwritten, appended or prepended.
* @param compress Tell if the content stream should compress the page contents.
* @param resetContext Tell if the graphic context should be reset. This is only relevant when
* the appendContent parameter is set to {@link AppendMode#APPEND}. You should use this when
* appending to an existing stream, because the existing stream may have changed graphic
* properties (e.g. scaling, rotation).
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
boolean compress, boolean resetContext) throws IOException
Thus因此
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
should make your code work as desired.应该使您的代码按需要工作。
Alternatively, if you want the image in the background, try或者,如果您想要背景中的图像,请尝试
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.PREPEND, true, true);
Beware, though, in certain cases the image won't be visible in the background, eg if the existing content starts with an instruction to fill the whole page area in white.但是请注意,在某些情况下,图像在背景中不可见,例如,如果现有内容以用白色填充整个页面区域的指令开始。 In such a case watermarks must be applied with some kind of transparency atop existing content.
在这种情况下,水印必须以某种透明度应用在现有内容之上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.