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