Using PdfBox, how do I retrieve contents of PDDocument as a byte array?

Gabriel Ruiu picture Gabriel Ruiu · Jul 21, 2012 · Viewed 16.8k times · Source

I am currently using PdfBox as the driver for a pdf-file editor application. I need the contents of the PdfBox representation of a pdf file (PDDocument) as a byte array. Does anyone know how to do this?

Answer

Ian Traum picture Ian Traum · Jul 15, 2013

I hope it's not too late...

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.save(byteArrayOutputStream);
document.close();
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

And voila! You've got both input streams!