title: Unable to merge streams with PDFBox
body: <p>Currently i am able to merge two PDF files when using java.io.File but unable to do merge them when using input and outputstreams. </p>
  
  <p>Below the code works and generates merged PDF with success.</p>
  
  <pre><code>File mainDoc = new File(path...);
  File additionalDoc = new File(path...);
  
  PDFMergerUtility pdfMerger = new PDFMergerUtility();
  pdfMerger.setDestinationFileName(path + "/merged.pdf");
  pdfMerger.addSource(mainDoc);
  pdfMerger.addSource(additionalDoc);
  pdfMerger.mergeDocuments(null);
  </code></pre>
  
  <p>I then tried to do the same by using streams. </p>
  
  <pre><code>ByteArrayOutputStream out = new ByteArrayOutputStream();
  InputStream mainDocStream = new FileInputStream(path...);
  InputStream additionalDocSteam = new FileInputStream(path...);
  PDFMergerUtility pdfMerger = new PDFMergerUtility();
  pdfMerger.addSource(mainDocStream);
  pdfMerger.addSource(additionalDocSteam);
  pdfMerger.setDestinationStream(out);
  pdfMerger.mergeDocuments(null);
  </code></pre>
  
  <p>The code above when reaching line <em>pdfMerger.mergeDocuments(null);</em> throws following exception : </p>
  
  <blockquote>
    <p>java.io.IOException: Error: End-of-File, expected line    at
    org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1119)
      at
    org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:2005)
      at
    org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:1988)
      at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:269)
      at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1143)
      at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1059)
      at
    org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:263)</p>
  </blockquote>
  
  <p>At last i tried to follow this answer (<a href="https://stackoverflow.com/a/37595137/1918516">Merge Pdf Files Using PDFBox</a>) as an example but my generated pdf does not seem to have merged those two pdfs.
  This is the code that i tried.</p>
  
  <pre><code>public InputStream createPDF() {
      try{
          // Note, i have also tried to use java.io.File instead of an 
          // InputStream but the result was the same
          // File mainDoc = new File(path...);
          // PDDocument document = PDDocument.load(mainDoc);
          InputStream pdfInputStream = null;
          InputStream mainDocStream = new FileInputStream(path...);
          PDDocument document = PDDocument.load(mainDocStream);
          InputStream additionalDocSteam = new FileInputStream(path...);
          PDDocument additionalDocument = PDDocument.load(additionalDocSteam);
          PDFMergerUtility pdfMerger = new PDFMergerUtility();
  
          pdfMerger.appendDocument(additionalDocument, document);
          document.save(out);
          document.close();
          PDDocument.load(out.toByteArray());
  
          pdfInputStream = new ByteArrayInputStream(out.toByteArray());
  
      }catch(...){
          ....
      }
      return pdfInputStream;
  } 
  </code></pre>
  
  <p>The code above does generate a PDF but the newly created PDF contains only the content of the main document and not from the second one. So it looks that i am missing something and the documents are not merged. </p>