IText merge with pdf tags - Recovery error: trailer not found; Original post: PDF startxref not found

We use iTextversion 5.5 PdfCopyto merge several tagged PDF files. (Those labeled PDFs are not created using iText.) We got the following error, but document.close:

java.lang.NullPointerException
at com.itextpdf.text.pdf.RefKey.<init>(RefKey.java:59)
at com.itextpdf.text.pdf.PdfCopy.fixTaggedStructure(PdfCopy.java:822)
at com.itextpdf.text.pdf.PdfCopy.flushTaggedObjects(PdfCopy.java:779)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:854)
at com.itextpdf.text.Document.close(Document.java:416)
com.itextpdf.text.exceptions.InvalidPdfException: Rebuild failed: trailer not found.;    Original    message: PDF startxref not found.
at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:668)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:181)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:230)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)

PDFs are valid and contain a trailer and the %EOFfiles are not damaged. What can this do different?

+4
source share
1 answer

You may already have found the answer. I had the same problem and solved it by closing the document before creating PdfReader.

Example:

**

Document tempDoc = new Document(PageSize.A4, 40, 40, 80, 20);
ByteArrayOutputStream tempBaos = new ByteArrayOutputStream();   
PdfWriter.getInstance(tempDoc, tempBaos);
tempDoc.open();

tempDoc.add(tempChap);
tempDoc.close();    // Closing the document before calling Reader

PdfReader reader = new PdfReader(tempBaos.toByteArray());

**

Hope this helps

+9
source

Source: https://habr.com/ru/post/1531510/


All Articles