当前位置: 首页 > 知识库问答 >
问题:

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

桓信鸥
2023-03-14

我使用的代码是: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个答案

戴高远
2023-03-14

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

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

 类似资料:
  • 我正在使用iText生成Pdf。但当我试图在pdf中添加图像时, 我mage.get实例(新的URL(timetableResource.getImageUrl()));document.add(学校标志); 但我得到的错误是 HTTP状态500-服务器为URL返回了HTTP响应代码400:http://139.59.72.150:8080/sms/attachments/23/42/school

  • 当我试图使用pdfBox绘制png图像时,页面保持空白。有什么方法可以使用PDFBOX插入png图像吗?

  • 我正在尝试使用Apache PDFBox库以编程方式创建PDF文档。PDPageContentStream类包含写入文本、绘制直线、贝塞尔曲线和矩形的方法。但我找不到一种方法来画一个简单的实心圆。有没有办法用这个库来绘制它?如果没有,您能否建议一个免费的Java库,它提供灵活的API以编程方式创建PDF文档?提前谢谢。

  • 我试图使用itext7将图像添加到我的pdf文档中,但我得到了DirectoryNotFoundExctive。我使用的是itextSharp,但itext7的情况并非如此,StackOverflow的一些类似问题向我展示了它是如何完成的,但它不会读取资产文件夹中的文件。这是我的代码: 当我使用按钮点击事件运行此代码时,它会抛出以下错误:

  • 上周,我们从itext 5.3.6升级到5.5.6,在测试过程中,我们发现在启用完全压缩的现有pdf上添加图像存在问题。请参见下一个代码示例:

  • 我是新来的。我有一个不同内容的文档列表,我想合并成一个单独的PDF。内容类型为PDF、JPG 这是我的代码: 合并PDF只工作得很好,但每次我合并一个图像我得到这个: 带有粉红色的图像被放置在先前PDF的文本上