小编Raj*_*Raj的帖子

将图像插入现有的非空 pdf

我正在尝试使用 apache PDFBOX 将图像保存到现有的 pdf 中,但我的内容被删除,当我将图像放在 pdf 上时,我得到空白文档,有解决问题的方法吗?

我的代码看起来像这样。

    public class TestPdfImage {
        public static void main(String args[]) throws Exception {
              //Loading an existing document
              File file = new File("...../mydoc.pdf");
              PDDocument doc = PDDocument.load(file);

              //Retrieving the page
              PDPage page = doc.getPage(0);

              //Creating PDImageXObject object
              PDImageXObject pdImage = PDImageXObject.createFromFile("...../sample.png",doc);

              //creating the PDPageContentStream object
              PDPageContentStream contents = new PDPageContentStream(doc, page);

              //Drawing the image in the PDF document
              contents.drawImage(pdImage, 70, 250);

              System.out.println("Image inserted");

              //Closing the PDPageContentStream object
              contents.close();     

              //Saving the document
              doc.save(".../sample.pdf");

              //Closing …
Run Code Online (Sandbox Code Playgroud)

java pdf image pdfbox

2
推荐指数
1
解决办法
967
查看次数

标签 统计

image ×1

java ×1

pdf ×1

pdfbox ×1