iTextSharp exception: PDF header signature not found

匿名 (未验证) 提交于 2019-12-03 01:31:01

问题:

I'm using iTextSharp to read the contents of PDF documents:

  PdfReader reader = new PdfReader(pdfPath);                  using (StringWriter output = new StringWriter())                 {                     for (int i = 1; i 

99% of the time it works just fine. However, there is this one PDF file that will sometimes throw this exception:

PDF header signature not found. StackTrace: at iTextSharp.text.pdf.PRTokeniser.CheckPdfHeader() at iTextSharp.text.pdf.PdfReader.ReadPdf() at iTextSharp.text.pdf.PdfReader..ctor(String filename, Byte[] ownerPassword) at Reader.PDF.DownloadPdf(String url) in C:\Documents\Visual Studio

What's annoying is that I can't always reproduce the error. Sometimes it works, sometimes it doesn't. Has anyone encountered this problem?

回答1:

After some research, I've found that this problem relates to either a file being corrupted during PDF generation, or an error related to an object in the document that doesn't conform to the PDF standard as implemented in iTextSharp. It also seems to happen only when you read from a PDF file from disk.

I have not found a complete solution to the problem, but only a workaround. What I've done is read the PDF document using the PdfReader itextsharp object and see if an error or exception happens before reading the file in a normal operation.

So running something similar to this:

private bool IsValidPdf(string filepath) {     bool Ret = true;      PdfReader reader = null;      try     {         reader = new PdfReader(filepath);     }     catch     {         Ret = false;     }      return Ret; } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!