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

时间:2018-11-13 22:21:23

标签: java apache pdf pdfbox

我正在使用以下代码: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();
    }

据我所知,我在做什么应该行得通,而且我没有收到任何错误,那怎么办?

1 个答案:

答案 0 :(得分:1)

也请查看您尝试使用的JavaDocs和库的源代码。您创建一个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);

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