title: How to get byte array from iText PDFReader
body: <p>How to get byte array from Itext PDFReader.</p>
  
  <pre><code>float width = 8.5f * 72;
  float height = 11f * 72;
  float tolerance = 1f;
  
  PdfReader reader = new PdfReader("source.pdf");
  
  for (int i = 1; i &lt;= reader.getNumberOfPages(); i++)
  {
      Rectangle cropBox = reader.getCropBox(i);
      float widthToAdd = width - cropBox.getWidth();
      float heightToAdd = height - cropBox.getHeight();
      if (Math.abs(widthToAdd) &gt; tolerance || Math.abs(heightToAdd) &gt; tolerance)
      {
          float[] newBoxValues = new float[] { 
              cropBox.getLeft() - widthToAdd / 2,
              cropBox.getBottom() - heightToAdd / 2,
              cropBox.getRight() + widthToAdd / 2,
              cropBox.getTop() + heightToAdd / 2
          };
          PdfArray newBox = new PdfArray(newBoxValues);
  
          PdfDictionary pageDict = reader.getPageN(i);
          pageDict.put(PdfName.CROPBOX, newBox);
          pageDict.put(PdfName.MEDIABOX, newBox);
      }
  }
  </code></pre>
  
  <p>From above code I need to get byte array from reader object. How?</p>
  
  <p>1) Not working, getting empty byteArray.</p>
  
  <pre><code>OutputStream out = new ByteArrayOutputStream();
  PdfStamper stamper = new PdfStamper(reader, out);
  stamper.close();
  
  byte byteArray[] = (((ByteArrayOutputStream)out).toByteArray()); 
  </code></pre>
  
  <p>2)  Not working, getting java.io.IOException: Error: Header doesn't contain versioninfo</p>
  
  <pre><code>ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
      for (int i = 1; i &lt;= reader.getNumberOfPages(); i++)
          {
              outputStream.write(reader.getPageContent(i));
          }
     PDDocument pdDocument = new PDDocument().load(outputStream.toByteArray( );)  
  </code></pre>
  
  <p>Is there any other way to get byte array from PDFReader.</p>