29

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:

PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page); 
PDPageContentStream contentStream = new PDPageContentStream(document, page);

/* ... */ 
/* code to add some text to the page */
/* ... */

InputStream in = new FileInputStream(new File("c:/myimg.jpg"));
PDJpeg img = new PDJpeg(document, in);
contentStream.drawImage(img, 100, 700);
contentStream.close();
document.save("c:/mydoc.pdf");

When I run the code, it terminates successfully, but if I open the generated pdf file using Acrobat Reader, the page is completely white and the image is not placed in it.
The text instead is correctly placed in the page.

Any hint on how to put my image in the pdf?

Davide Gualano
  • 12,813
  • 9
  • 44
  • 65

3 Answers3

53

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 );
...
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • 4
    Moving the PDJpeg creation before the PDPageContentStream resolved my issue, thank you very much. – Davide Gualano Dec 22 '11 at 09:11
  • 1
    Dah, I've been battling with this one as well and this solved it, thanks. Seems like a bug in my book... should at least give a warning! – Michael Berry Dec 31 '12 at 17:45
  • You saved me! Tnx. Worked an hour to figure it out why my image is not displayed from a ByteArrayInputStream ... was the order of PDJpeg and PDPageContentStream ... – Mircea Stanciu Feb 05 '13 at 07:51
  • 2
    I found that it is only the first `PDJpeg` created after the `PDPageContentStream`. I was writing 3 images and the first one would not show on the pdf but the other two had no problem. To solve this, I create the first `PDJpeg`, and then I create a new `PDJpeg` object with the same image and write it. Then I write the other 2 images without problem. Inconvenient, but it works. – Niro Oct 29 '13 at 19:35
  • Thank you! me and my colleague were going mad :) – lbrutti Jan 09 '14 at 11:47
6

Looks like you're missing just a document.addPage(page) call.

See also the ImageToPDF example class in PDFBox for some sample code.

Jukka Zitting
  • 1,092
  • 6
  • 13
  • The page is added to the document in the actual code, I forgot to paste that line of code, I'm correcting the original post. – Davide Gualano Dec 22 '11 at 09:08
2

this is how default constructor for PDPageContentStream looks like:

public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException
{
    this(document, sourcePage, AppendMode.OVERWRITE, true, false);
}

Problem is AppendMode.OVERWRITE for me using another constructor with parameter PDPageContentStream.AppendMode.APPEND resolved a problem

For me this worked:

PDPageContentStream contentStream =
        new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);
Piotr Żak
  • 2,083
  • 6
  • 29
  • 42