title: How to merge two PDF files into one in Java?
body: <p>I want to merge many PDF files into one using <a href="http://pdfbox.apache.org/" rel="noreferrer">PDFBox</a> and this is what I've done:</p>
  
  <pre><code>PDDocument document = new PDDocument();
  for (String pdfFile: pdfFiles) {
      PDDocument part = PDDocument.load(pdfFile);
      List&lt;PDPage&gt; list = part.getDocumentCatalog().getAllPages();
      for (PDPage page: list) {
          document.addPage(page);
      }
      part.close();
  }
  document.save("merged.pdf");
  document.close();
  </code></pre>
  
  <p>Where <code>pdfFiles</code> is an <code>ArrayList&lt;String&gt;</code> containing all the PDF files.</p>
  
  <p>When I'm running the above, I'm always getting:</p>
  
  <pre><code>org.apache.pdfbox.exceptions.COSVisitorException: Bad file descriptor
  </code></pre>
  
  <p>Am I doing something wrong?  Is there any other way of doing it?</p>