0

I am using itext in Java for updating a pdf file.

  • I read the pdf
  • I update the content (get PRStream, convert into stream then string, update the string, convert it back PRStream)
  • I regenerate the output file using

    pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("new-file.pdf"));
    

It's working all fine, now I want to send the stream as output to a rest API call, how can I convert pdfReader to byte[]

I have been able to save the file and then read the stream from it using

 byte[] stream = Files.readAllBytes(file.toPath());

but I don' want to save the file, I want to retrieve the stream directly instead of saving the file and then getting byte[] of the newly generated pdf

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • 1
    Have you tried using a ByteArrayOutputStream instead of a FileOutputStream, and then getting the bytes from the ByteArrayoutputStream? – Ben Ingle Mar 28 '19 at 11:17
  • 3
    Something like: `ByteArrayOutputStream bos = new ByteArrayOutputStream(); pdfStamper = new PdfStamper(pdfReader, bos); return bos.toByteArray();` – Codo Mar 28 '19 at 13:26
  • @Codo For your code proposal to work, there must be a `pdfStamper.close()` before `return bos.toByteArray()`. (The OP also forgot to mention that closing.) – mkl Mar 28 '19 at 17:17
  • @Ben or Codo, you may want to make that an actual answer. – mkl Mar 28 '19 at 17:19

0 Answers0