How to convert pdfstamper to byte array

前端 未结 1 1712
北荒
北荒 2021-01-24 19:35

In my application, i need to read the existing pdf and add barcode to the existing PDF and pass it to output stream. here the existing pdf is like template. I am using iText jar

1条回答
  •  -上瘾入骨i
    2021-01-24 19:58

    Your question is unclear. I assume that you want to write to a ByteArrayOutputStream instead of to a FileOutputStream. There are different examples on how to do that on the iText web site.

    See for instance the FormServlet example where it says:

    // We create an OutputStream for the new PDF
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Now we create the PDF
    PdfStamper stamper = new PdfStamper(reader, baos);
    

    Then later in the example, we do this:

    // We write the PDF bytes to the OutputStream
    OutputStream os = response.getOutputStream();
    baos.writeTo(os);
    

    If you want a byte[], you can simply do this:

    byte[] pdfBytes = baos.toByteArray();
    

    I hope your question wasn't about writing a PdfContentByte stream to a byte[] because that wouldn't make sense: a content stream doesn't contain any resources such as fonts, images, form XObjects, etc...

    0 讨论(0)
提交回复
热议问题