iTextSharp的例外:PDF标题签字未找到 [英] iTextSharp exception: PDF header signature not found

查看:1785
本文介绍了iTextSharp的例外:PDF标题签字未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp的阅读PDF文档的内容:

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 <= reader.NumberOfPages; i++)
                        output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));

                    reader.Close();
                    pdfText = output.ToString();
                }

99%只是正常工作的时间。然而,这有时会抛出此异常这一个PDF文件:

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

Message: 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?

推荐答案

经过一番研究,我发现,这个问题涉及到一个文件中生成PDF文件被损坏,或文档中涉及到的对象的错误不符合PDF格式标准如iTextSharp的实施。这也似乎发生,只有当你从磁盘上的PDF文件阅读。

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.

我还没有找到一个完整的解决问题的方法,但是只有一种解决方法。我所做的就是读取使用PdfReader iTextSharp的对象,并查看是否有错误或异常读取该文件在正常操作之前发生的PDF文档。

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.

所以,运行类似这样:

private bool IsValidPdf(string filepath)
{
    bool Ret = true;

    PdfReader reader = null;

    try
    {
        reader = new PdfReader(filepath);
    }
    catch
    {
        Ret = false;
    }

    return Ret;
}

这篇关于iTextSharp的例外:PDF标题签字未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆