title: How to make streamed PDF merging without memory consumption?
body: <p>I need to merge many small pdf files to one big pdf (~200G). And I can't find library/tool that can do it without eating all my memory.</p>
  
  <p>I watched to itext, pdfbox, pdftk. But seems that all of them store file in memory. According to pdf file structure it should be very easy sequentially take input documents streams and write to result file, keeping in memory only xref table.</p>
  
  <p>Code I used to test iText. With every next file it consumes more and more memory:</p>
  
  <pre><code>public static void MergePDFs(String[] fileNames, String targetPdf) throws IOException, DocumentException {
      FileOutputStream stream = new FileOutputStream(targetPdf);
      Document document = new Document();
      PdfCopy pdf = new PdfCopy(document, stream);
      PdfReader reader = null;
      document.open();
      for (String file : fileNames) {
          reader = new PdfReader(file);
          pdf.addDocument(reader);
          pdf.freeReader(reader);
          reader.close();
      }
      if (reader != null) {
          reader.close();
      }
      document.close();
      stream.close();
  }
  </code></pre>