Can't add an image to a pdf using PDFBox

后端 未结 3 1587
再見小時候
再見小時候 2020-12-10 10:45

I\'m writing a java app that creates a pdf from scratch using the pdfbox library.
I need to place a jpg image in one of the page.

I\'m using this code:



        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 11:09

    Definitely add the page to the document. You'll want to do that, but I've also noticed that PDFBox won't write out the image if you create the PDPageContentStream BEFORE the PDJpeg. It's unexplained why this is so, but if you look close at the source of ImageToPDF that's what they do. Create the PDPageContentStream after PDJpeg and it magically works.

    ...
    PDJpeg img = new PDJpeg(document, in);
    PDPageContentStream stream = new PDPageContentStream( doc, page );
    ...
    

提交回复
热议问题