Can't add an image to a pdf using PDFBox

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 );
...

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

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

Tags:

Java

Pdf

Pdfbox