Reputation: 125
I have a simple function that is merging together pdfs from an ArrayList using iText7
File pdfMerged = new File("merged.pdf");
PdfDocument pdf = new PdfDocument(new PdfWriter(pdfMerged));
PdfMerger merger = new PdfMerger(pdf);
for (InputStream in : files) {
//Add pages from the each document
PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
sourcePdf.close();
}
pdf.close();
}
While this works most of the time, unfortunately one pdf is giving the following error.
2020-08-28 18:05:59,935 ERROR [kernel.pdf.PdfReader] [http-nio-8080-exec-9] Error occurred while reading cross reference table. Cross reference table will be rebuilt.
com.itextpdf.io.IOException: Error at file pointer 1,051,972.
at com.itextpdf.io.source.PdfTokenizer.throwError(PdfTokenizer.java:639)
at com.itextpdf.kernel.pdf.PdfReader.readXrefSection(PdfReader.java:839)
at com.itextpdf.kernel.pdf.PdfReader.readXref(PdfReader.java:777)
at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:532)
at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1638)
at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:231)
...
...
...
Caused by: com.itextpdf.io.IOException: file position {0} cross reference entry in this xref subsection.
The pdf used in the merge that's causing issues opens just fine in other programs. It is only itext7 that is complaining.
I can't share the problematic pdf, but I can say it's using PDF version 1.6.
This causes the result to be an empty pdf. What does this error mean and how can I fix it or work around it?
Upvotes: 3
Views: 3518
Reputation: 125
I found out that merger is actually working and merging the files but is throwing the exception anyway. I guess to inform the developer or user it is doing a fix? If you read the exception carefully it says the "Cross reference table will be rebuilt." So iText is doing some kind of automated fix.
This exception was triggering a different try catch block and the pdf was never being closed as a result.
Strangely I couldn't find any documentation on this in the iText 7 documentation.
At any rate, a simple try catch around the merge function fixed the issue in my case.
for (InputStream in : files) {
//Add pages from the each document
PdfDocument sourcePdf = new PdfDocument(new PdfReader(in));
try{
merger.merge(sourcePdf, 1, sourcePdf.getNumberOfPages());
} catch (Exception e) {
//log an error and continue
}
sourcePdf.close();
}
Upvotes: 3