1
votes

We are trying to upgrade from iText 5 to iText 7 and saw few issues. I am getting an exception as "com.itextpdf.io.IOException: PDF startxref not found." inside PdfReader#readPdf() and finally in the caller method getting an exception as "com.itextpdf.kernel.PdfException: Trailer not found.".

My use case is creating PdfReader instance using inputSream and then creating PdfDocument from the reader and passing PdfWriter as a constructor parameter.We are trying to modify existing pdf, the sample code is as below

PdfReader pdfReader = new PdfReader(inputStream);
pdfReader.setUnethicalReading(true);
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfDocument pdfDocument = new PdfDocument(pdfReader, new PdfWriter(os));

Can someone, please suggest what I am doing wrong and how we can fix this issue. We have a utility method that writes the output stream and creates a new pdf attachment.

1
Where does inputStream come from? Are you sure you are reading a correct PDF file (i.e. you can open it with an available PDF viewer tool)? Can you share a sample PDF that you have problems with? - Alexey Subach
Simply said the error messages mean that your InputStream is not positioned at the start of a pdf. I.e. either there is no valid pdf in it at all or the stream already is (partially or completely) read. - mkl
@Alexy- The pdf is attached as an attachment to an existing table, we have an api through which we read the attachment as an InputStream. I guess, the pdf that was getting created was corrupted. I need to look into that code how I am setting the values in the Pdf Fields. - Varun Jaiswal
Can you post the solution as an answer? That way people can see that the question has been answered (and that they can skip the question). - Bruno Lowagie
I was not closing the stream properly, I created the pdfDocument instance and reading from output stream before closing the pdfDocument. So need to close pdfDocument stream first and then read from output stream to create attachments. - Varun Jaiswal

1 Answers

4
votes

Copying the answer from the comments:

I got this issue fixed, I need to close pdfDocument before I am writing attachments from the output stream.

I was not closing the stream properly, I created the pdfDocument instance and reading from output stream before closing the pdfDocument. So I need to close pdfDocument stream first and then read from output stream to create attachments.