【问题标题】:Problem with empty page when using Apache PDFBox to add image to PDF使用 Apache PDFBox 将图像添加到 PDF 时出现空白页面的问题
【发布时间】:2019-04-16 20:10:53
【问题描述】:

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

据我所知,我正在做的事情应该可以工作,而且我没有收到任何错误,那是什么?

【问题讨论】:

  • 可能与 stackoverflow.com/questions/27919436 重复。请先尝试,如果不起作用,请链接到您的 PDF。
  • @TilmanHausherr 不是重复的,而是密切相关的。这里的问题是由于使用new PDPageContentStream(doc, page) 而导致的失败,解决方案是使用至少具有第三个(附加)参数的构造函数。您链接到的问题中的问题是无法使用new PDPageContentStream(doc, page, true, true),解决方案是使用带有第五个(resetContext)参数的构造函数。人们可能会考虑为所有关于使用哪个PDPageContentStream 构造函数的问题创建一个规范的答案。

标签: java apache pdf pdfbox


【解决方案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);

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

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2012-01-21
    • 2019-08-11
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多