title: Converting pdfDocument to byte[] stream - PDFBox Java
body: <p>I am updating the values of an editable PDF using <code>PDFBox</code>. Instead of saving, I want to return stream. I saved it, it works all fine. Now I want to return <code>byte[]</code> instead of saving it.</p>
  
  <pre><code>public static void main(String[] args) throws IOException
  {
      String formTemplate = "myFormPdf.pdf";
  
      try (PDDocument pdfDocument = PDDocument.load(new File(formTemplate)))
      {
          PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
  
          if (acroForm != null)
          {
  
              PDTextField field = (PDTextField) acroForm.getField( "sampleField" );
              field.setValue("Text Entry");
          }
  
          pdfDocument.save("updatedPdf.pdf"); // instead of this I need STREAM
      }
  }
  </code></pre>
  
  <p>I tried <code>SerializationUtils.serialize</code> but it fails to serialize it.</p>
  
  <pre><code>Failed to serialize object of type: class org.apache.pdfbox.pdfmodel.PDDcoumemt
  </code></pre>