Rajesh Arumugam
Rajesh Arumugam

Reputation: 1

iText PDF Concatination fails - InvalidPDFException

I am trying to concatenate 2 PDFs using itext 4.2.0 utility. For few cases, it throws InvalidPDFException in below code

reader = new PdfReader("c:\tmp\test.pdf");

com.itextpdf.text.exceptions.InvalidPdfException: No message found for trailer.not.found at com.itextpdf.text.pdf.PdfReader.rebuildXref(Unknown Source) at com.itextpdf.text.pdf.PdfReader.readPdf(Unknown Source) at com.itextpdf.text.pdf.PdfReader.(Unknown Source) at com.itextpdf.text.pdf.PdfReader.(Unknown Source)

This PDF is valid one- I opened it in Text editor and ensured it has %PDF as well as %EOF as recommended here

UPDATE

The iText version is 2.1.7. The jar was wrongly named as 4.2.0.

The path mentioned ("c:\tmp\test.pdf") is sample one. We are sending as "c:/tmp/test.pdf"

Upvotes: 0

Views: 441

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77546

  1. There is no iText 4.2.0. Please throw it away. It is a rogue version that is not released by the official developers of iText. It's a "gork", meaning God Only Really Knows what's inside. Solution: Throw away iText 4.2.0 and replace it with a more recent, official version: https://github.com/itext/itextpdf/releases
  2. You get the error saying that the actual error message for the key trailer.not.found is not found. This means that you are using an iText jar that isn't build correctly. The .lng files are missing from the jar, hence the actual error message can't be found. Solution: Throw away iText 4.2.0 and replace it with a more recent, official version: https://github.com/itext/itextpdf/releases
  3. The key trailer.not.found corresponds with the message "Trailer not found". It means that you are trying to create a PdfReader with a file that may look like a PDF, but that isn't. For instance: it starts with %PDF-, but there is no trailer. That means that iText searches the file (that should end in %%EOF; please check if this is the case) and the keyword startxref can be found. In other words: the trailer is missing. Solution: check if the PDF is valid. Note that old versions of iText weren't able to read PDFs that use a feature that was introduced after version PDF 1.5. Maybe your "unofficial" iText version is that old...
  4. Finally: \ is an escape character. This is wrong: "c:\tmp\test.pdf" because if reads as "c:[tab] mp [tab] est.pdf" where [tab] is the tab character \t. You should use either "c:/tmp/test.pdf" or "c:\\tmp\\test.pdf".

Upvotes: 2

Related Questions