How to get the OutputStream of a written PDF from iText

I need your help in getting and storing the written PDF from iText in an OutputStream and then to convert it to an InputStream.

The code of writing the PDF is below:

public void  CreatePDF() throws IOException {
      try{
        Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
        OutputStream out = new ByteArrayOutputStream();            
        PdfWriter writer = PdfWriter.getInstance(doc, out);

        doc.open();
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
        table.addCell(cell);
        doc.add(table);
        doc.close();

      }
        catch (Exception e) {
          e.printStackTrace();
                }
    } 

So I am seeking your help to write that PDF in an OutputStream and then to convert it to InputStream.

I need to get the InputStream value so I can pass it to the line for the file download:

StreamedContent file = new DefaultStreamedContent(InputStream, "application/pdf", "xxx.pdf");

Updated Jon Answer:

public InputStream createPdf1() throws IOException {
    Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
    try {        

    ByteArrayOutputStream out = new ByteArrayOutputStream();            
        PdfWriter writer;
            try {
                writer = PdfWriter.getInstance(doc, out);
            } catch (DocumentException e) {
            }
            doc.open();
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
        table.addCell(cell);
        doc.add(table);

        }
        catch ( Exception e)
        {
        e.printStackTrace();
        }
    return new ByteArrayInputStream(out.toByteArray());
}
Jon Skeet
people
quotationmark

You should change the declaration of out to be of type ByteArrayOutputStream rather than just OutputStream. Then you can call ByteArrayOutputStream.toByteArray() to get the bytes, and construct a ByteArrayInputStream wrapping that.

As an aside, I wouldn't catch Exception like that, and I'd use a try-with-resources statement to close the document, assuming it implements AutoCloseable. It's also a good idea to follow Java naming conventions. So for example, you might have:

public InputStream createPdf() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();            
    try (Document doc = new Document(PageSize.A4, 50, 50, 50, 50)) {
        PdfWriter writer = PdfWriter.getInstance(doc, out);
        doc.open();
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("First PDF"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
        table.addCell(cell);
        doc.add(table);
    }
    return new ByteArrayInputStream(out.toByteArray());
}

people

See more on this question at Stackoverflow