Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IText merge tagged pdf - Rebuild failed: trailer not found.; Original message: PDF startxref not found

Tags:

itext

We use iText version 5.5 PdfCopy to merge multiple tagged PDFs. (Those tagged PDFs are not created by iText.) We got the following error while 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)

PDF are valid and contains trailer and %EOF, Files are not corrupted. What other could cause this?

like image 355
user3413999 Avatar asked Mar 13 '14 07:03

user3413999


1 Answers

You may have found the answer already. I had the same issue and I solved it by closing the document before instantiating 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 will help

like image 52
OrangeTree Avatar answered Jan 03 '23 20:01

OrangeTree