1

I need to split or merge some pdf and I have some rare pdf that create the following exception.

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)

After a bit of searching, i found this post about a similar problem :

Itext7 generate pdf with Exception "Pdf indirect object belongs to other PDF document. Copy object to current pdf document."

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).

My code is the following :

public static void splitByPage(File pdfToSplit, int nbPageByPDF){
    try {
        // Open the document in reading mode
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfToSplit));

        List<PdfDocument> 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();

    }

This code is inspired from this example : https://developers.itextpdf.com/examples/stamping-content-existing-pdfs/clone-splitting-pdf-file

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).

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.

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.

Thanks in advance for any help or suggestion.

K.Georges
  • 13
  • 4
  • Please share the PDF in question. If absolutely necessary, you can do so via mail. You'll find an e-mail address in [my profile here](https://stackoverflow.com/users/1729265/mkl?tab=profile). – mkl May 28 '18 at 13:25
  • The pdf is from the company (APSYS) and a bit sensitive, so the pdf has been sent to the e-mail address. As it is a 60Mo file, we sent you a link to download it. – K.Georges May 30 '18 at 08:13
  • The issue already appears to be fixed. I tested your code and your file with the recent 7.1.2 and could reproduce the issue. Then I repeated the test with the current 7.1.3-SNAPSHOT development state and the error did not occur anymore. – mkl May 30 '18 at 10:59
  • Ok, thanks for letting me know about that. Do you have any information about the release date of the 7.1.3 version ? – K.Georges May 30 '18 at 11:07
  • Unfortunately I don't know; but I think the release date is pretty near. – mkl May 30 '18 at 11:39

1 Answers1

1

This issue has already been fixed in the current 7.1.3-SNAPSHOT development version state. More exactly, it has been fixed in commit 251606e55768a47cb68eb8c58f2f5fe36324d85b dated 2018-04-23 13:46:25 in the course of resolving issue DEVSIX-1913 (Fix copying of inherited page entries).

The cause was that in PdfPage.copyInheritedProperties(PdfPage, PdfDocument) for some properties the values were added as-is to the target document.

This is ok for direct objects; as in most PDFs the values of those properties are direct objects, this had gone unnoticed for quite some time.

The page property in question is the CropBox which in your example document happens to be inherited from the root of the pages tree and to have an indirect value.

Thus, you may either

  • wait for a 7.1.3 release, or
  • use a 7.1.3-SNAPSHOT, or
  • backport the fix to your iText version. In this case you merely have to replace

    copyPdfPage.put(PdfName.CropBox, cropBox);
    

    by

    copyPdfPage.put(PdfName.CropBox, cropBox.copyTo(pdfDocument));
    

    in PdfPage.copyInheritedProperties(PdfPage, PdfDocument).

mkl
  • 90,588
  • 15
  • 125
  • 265