title: How to get the OutputStream of a written PDF from iText
body: <p>I need your help in getting and storing the written PDF from iText in an <code>OutputStream</code> and then to convert it to an <code>InputStream</code>.</p>
  
  <p>The code of writing the PDF is below:</p>
  
  <pre><code>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();
                  }
      } 
  </code></pre>
  
  <p>So I am seeking your help to write that PDF in an <code>OutputStream</code> and then to convert it to <code>InputStream</code>.</p>
  
  <p>I need to get the <code>InputStream</code> value so I can pass it to the line for the file download:</p>
  
  <pre><code>StreamedContent file = new DefaultStreamedContent(InputStream, "application/pdf", "xxx.pdf");
  </code></pre>
  
  <p>Updated Jon Answer:</p>
  
  <pre><code>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());
  }
  </code></pre>