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?
Asked
Active
Viewed 2.8k times
29
-
did you ever find out? I'd like to know too... – Martin Charlesworth Dec 06 '12 at 22:02
1 Answers
63
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!

JJS
- 1,086
- 2
- 13
- 16
-
-
The answer is correct, at least for the version of PDFBox that I checked (1.8.4) – Gabriel Ruiu Feb 06 '14 at 08:58
-
-
1
-
You saved me. The PDFMergeUtility is an effin' mess in the 2.0.* and was unable to merge PDDocuments with Type0 Fonts. So I saved both files in a stream and then could merge them later as inputstreams. – ak.leimrey Jun 06 '21 at 21:57
-
@ak.leimrey - Do you mind sharing the bits you used for merging? Thanks – yardie Oct 22 '21 at 13:17