使用 Apache PDFBox 将图像添加到 PDF 时出现空页问题

Problem with empty page when using Apache PDFBox to add image to PDF

我正在使用此代码:https://www.tutorialspoint.com/pdfbox/pdfbox_inserting_image.htm

帮助我向现有 PDF 添加图像。问题是它创建的文件是一个空白页,上面只有图像。

这是我的代码:

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();
    }

据我所知,我所做的应该有效,而且我没有收到任何错误,那是怎么回事?

另请查看您尝试使用的库的 JavaDoc 和源代码。您创建一个 PDPageContentStream:

PDPageContentStream contents = new PDPageContentStream(doc, 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

因此,您必须使用不同的构造函数来保留当前页面内容,例如

/**
 * 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

因此

PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);

应该可以使您的代码按预期工作。

或者,如果您想要背景中的图像,请尝试

PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.PREPEND, true, true);

但请注意,在某些情况下,图片在背景中是不可见的,例如如果现有内容以用白色填充整个页面区域的指令开头。在这种情况下,水印必须以某种透明方式应用在现有内容之上。