iText pdfcopy close off by one error

609 views Asked by At

I see this error a lot in an application with a lot of traffic when dealing with a PDF merge request.

java.lang.RuntimeException: The page 3 was requested but the document has only 2 pages. at com.lowagie.text.pdf.PdfWriter.close(PdfWriter.java:1169) at com.lowagie.text.pdf.PdfCopy.close(PdfCopy.java:485) at ...

The error is in the PdfCopy class, addPage method:

public void addPage(PdfImportedPage iPage) throws IOException, BadPdfFormatException

pageRef = getCurrentPage(); // increase pageReferences.size()

But later on an IOException occurs (ClientAbortException), but the last line ++currentPageNumber; didn't get a chance to execute.

How do I prevent this from happening? Can I add a finally block in the last line?

0

There are 0 answers