title: Error about indirect object with some PDF when splitting / merging
body: <p>I need to split or merge some pdf and I have some rare pdf that create the following exception. </p>
  
  <pre><code>com.itextpdf.kernel.PdfException: Pdf indirect object belongs to other PDF document. Copy object to current pdf document.at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:216)
  at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:206)
  at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:112)
  at com.itextpdf.kernel.pdf.PdfWriter.writeToBody(PdfWriter.java:393)
  at com.itextpdf.kernel.pdf.PdfWriter.flushObject(PdfWriter.java:301)
  at com.itextpdf.kernel.pdf.PdfDocument.flushObject(PdfDocument.java:1743)
  at com.itextpdf.kernel.pdf.PdfObject.flush(PdfObject.java:183)
  at com.itextpdf.kernel.pdf.PdfObject.flush(PdfObject.java:152)
  at com.itextpdf.kernel.pdf.PdfObjectWrapper.flush(PdfObjectWrapper.java:94)
  at com.itextpdf.kernel.pdf.PdfPage.flush(PdfPage.java:505)
  at com.itextpdf.kernel.pdf.PdfPage.flush(PdfPage.java:462)
  at com.itextpdf.kernel.pdf.PdfDocument.close(PdfDocument.java:847)
  at testPDF.PDF.splitByPage(PDF.java:564)
  at testPDF.Main.main(Main.java:153)
  </code></pre>
  
  <p>After a bit of searching, i found this post about a similar problem :</p>
  
  <p><a href="https://stackoverflow.com/questions/44652992/itext7-generate-pdf-with-exception-pdf-indirect-object-belongs-to-other-pdf-doc">Itext7 generate pdf with Exception &quot;Pdf indirect object belongs to other PDF document. Copy object to current pdf document.&quot;</a></p>
  
  <p>In my case, I only split and merge the pdf, I don't touch to the content of the pdf, so I don't know why this exception happens. (From what I understood, the exception come from a problem in the copying of some font). </p>
  
  <p>My code is the following : </p>
  
  <pre><code>public static void splitByPage(File pdfToSplit, int nbPageByPDF){
      try {
          // Open the document in reading mode
          PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfToSplit));
  
          List&lt;PdfDocument&gt; splitDocuments = new PdfSplitter(pdfDoc) {
              int partNumber = 1;
  
              @Override
              protected PdfWriter getNextPdfWriter(PageRange documentPageRange) {
                  try {
                      return new PdfWriter(pdfToSplit.getAbsolutePath()
                                                     .substring(0,
                                                                pdfToSplit.getAbsolutePath()
                                                                          .lastIndexOf(".")
                                                                ) 
                                              + "splitPage_part" 
                                              + String.valueOf(partNumber++) 
                                              + ".pdf");
                  } catch (FileNotFoundException e) {
                      throw new RuntimeException();
                  }
              }
          }.splitByPageCount(nbPageByPDF);
  
          // Close all the part created
          for (PdfDocument doc : splitDocuments) {
              doc.close(); // exception throws at the first closing
          }
  
          // Close the initial pdf to split
          pdfDoc.close();
  
      }
  </code></pre>
  
  <p>This code is inspired from this example : <a href="https://developers.itextpdf.com/examples/stamping-content-existing-pdfs/clone-splitting-pdf-file" rel="nofollow noreferrer">https://developers.itextpdf.com/examples/stamping-content-existing-pdfs/clone-splitting-pdf-file</a></p>
  
  <p>For the merging, the same error happens when i try to close the new pdf where I appended the pdf that cause the exceptions. (But i can do it the other way. I can append another pdf (without this problem) to the pdf with the problem).</p>
  
  <p>I think that i need to find the way to copy the font directly from the initial pdf to each pdf i create, but i can't find the way to do it.</p>
  
  <p>If needed, i can send you in private the pdf with which the error occur, only in private since this pdf is a bit confidential.</p>
  
  <p>Thanks in advance for any help or suggestion.</p>