title: How to merge many pdfs
body: <p>I want to ask how to merge more than 100k pdf files (file each pdf around 160 KB ) into 1 pdf file ?</p>
  
  <p><a href="https://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-7-creating-pdfua-and-pdfa-documents" rel="nofollow noreferrer">Tutorial</a></p>
  
  <p>I already read this tutorial, that code is working for few pdf. But when I tried for 10k pdf files I get this error "java.lang.OutOfMemoryError: GC overhead limit exceeded"</p>
  
  <p>I already tried using -Xmx or -Xms, the error become "java heap space".</p>
  
  <p>I am also using "pdf.flushCopiedObjects(firstSourcePdf);" it doesn't help. Or maybe I am using it incorrectly?</p>
  
  <pre><code>File file = new File(pathName);
          File[] listFile = file.listFiles();
          if (listFile == null) {
              throw new Exception("File not Found at " + pathName);
          }
          Arrays.sort(listFile, 0, listFile.length - 1);
  
          PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
              PdfAConformanceLevel.PDF_A_1A,
              new PdfOutputIntent("Custom", "", "http://www.color.org",
                  "sRGB IEC61966-2.1", null));
  
          //Setting some required parameters
          pdf.setTagged();
          pdf.getCatalog().setLang(new PdfString("en-US"));
          pdf.getCatalog().setViewerPreferences(
              new PdfViewerPreferences().setDisplayDocTitle(true));
          PdfDocumentInfo info = pdf.getDocumentInfo();
          info.setTitle("iText7 PDF/A-1a example");
  
          //Create PdfMerger instance
          PdfMerger merger = new PdfMerger(pdf);
          //Add pages from the first document
  
          for (File filePdf : listFile) {
              System.out.println("filePdf = " +filePdf.getName());
              PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(filePdf));
              merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
              pdf.flushCopiedObjects(firstSourcePdf);
              firstSourcePdf.close();
          }
  
          pdf.close();
  </code></pre>
  
  <p>Thank You</p>