Why do I get an "InvalidPdfException: PDF header signature not found"?

I have some code that reads pdf files. The code fails at the line :

 

iTextSharp.text.pdf.PRTokeniser.CheckPdfHeader() at
                        iTextSharp.text.pdf.PdfReader.ReadPdf()
I know from other entries that this issue is coming from some invalid formatting in the PDF. However I'm not in a position to tell my users to redo their PDFs. Is there some other way around this issue, that can allow reading of the pdf despite this problem?

Posted on StackOverflow on Sep 10, 2012 by David Choi

If a file doesn't start with %PDF- then there's nothing to fix: the file isn't a PDF file.

However, there may be another problem: maybe you're trying to access a file that has zero length due to some problem while creating the InputStream. Another context in which I've seen this happen, is a PDF loaded from a server, where the server returned a 404 message in HTML instead of a PDF file ;-)

Whenever that exception happens, you should store the bytes somewhere, and examine them. Without those bytes, nobody will be able to give you useful advice.

Click this link if you want to see how to answer this question in iText 5.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now