简体   繁体   English

iTextSharp PDF阅读错误

[英]iTextSharp PDF Read Error

I have below code 我有下面的代码

using (var reader = new PdfReader(pdfPath))
{
    for (int pageIndex = 1; pageIndex <= reader.NumberOfPages; pageIndex++)
    {
        var text = PdfTextExtractor.GetTextFromPage(reader, pageIndex);
        //my other logic goes here
    }
}

I'm getting value cannot be null at the line 我正在获取价值,不能在行上为空

using (var reader = new PdfReader(pdfPath))

I'm not sure why it fails for a few PDFs. 我不确定为什么几个PDF都失败。 I'm able to read 100s of PDFs but only 4 PDFs, I get this error. 我能够读取100张PDF,但只有4张PDF,但出现此错误。

Error: 错误:

System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at System.util.collections.HashSet2`1.AddAndCheck(T item)
   at iTextSharp.text.pdf.PdfReader.PageRefs.IteratePages(PRIndirectReference rpage)
   at iTextSharp.text.pdf.PdfReader.PageRefs.IteratePages(PRIndirectReference rpage)
   at iTextSharp.text.pdf.PdfReader.PageRefs.IteratePages(PRIndirectReference rpage)
   at iTextSharp.text.pdf.PdfReader.PageRefs.IteratePages(PRIndirectReference rpage)
   at iTextSharp.text.pdf.PdfReader.PageRefs.IteratePages(PRIndirectReference rpage)
   at iTextSharp.text.pdf.PdfReader.PageRefs.ReadPages()
   at iTextSharp.text.pdf.PdfReader.PageRefs..ctor(PdfReader reader)
   at iTextSharp.text.pdf.PdfReader.ReadPages()
   at iTextSharp.text.pdf.PdfReader.ReadPdf()
   at iTextSharp.text.pdf.PdfReader..ctor(IRandomAccessSource byteSource, Boolean partialRead, Byte[] ownerPassword, X509Certificate certificate, ICipherParameters certificateKey, Boolean closeSourceOnConstructorError)
   at iTextSharp.text.pdf.PdfReader..ctor(String filename)

My iTextSharp version is 5.5.7.0 我的iTextSharp版本是5.5.7.0

The simplest reason would be that on those 4 PDFs, pdfPath is null instead of a string. 最简单的原因是在这4个PDF上,pdfPath为null而不是字符串。 Check for a null value in pdfPath. 检查pdfPath中是否为空值。

4个PDF的路径可能无效,这意味着那里没有PDF文件。

Just to close this topic, I have requested the PDF supplier to regenerate the files in question for me. 为了结束本主题,我已请求PDF供应商为我重新生成有问题的文件。 They did re-generate and send me and I'm able to process them without any code changes. 它们确实重新生成并发送给我,我可以在不进行任何代码更改的情况下对其进行处理。 It appears that there is something wrong in the PDF content that is not readable by iTextSharp properly. 似乎PDF内容中有问题,iTextSharp无法正确读取。 I still wonder because there was no change in their process and in our process. 我仍然想知道,因为他们的过程和我们的过程没有变化。 It may be a corrupted PDF somewhere. 某处可能是PDF损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM