C# iTextSharp PDF读取错误
我有下面的代码C# iTextSharp PDF读取错误,c#,.net,itextsharp,C#,.net,Itextsharp,我有下面的代码 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
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
}
}
我不知道为什么它会在一些PDF中失败。我能够读取100个PDF文件,但只有4个PDF文件,我得到了这个错误
错误:
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)
我的iTextSharp版本是5.5.7.0最简单的原因是在这4个PDF上,pdfPath是null而不是字符串。检查pdfPath中的空值。4个PDF的路径可能无效,这意味着那里没有PDF文件。为了结束本主题,我已请求PDF供应商为我重新生成有问题的文件。他们确实重新生成并发送给我,我能够在不更改任何代码的情况下处理它们。看起来PDF内容中有一些错误,iTextSharp无法正确读取。我仍然感到奇怪,因为他们的过程和我们的过程都没有改变。它可能是某个地方损坏的PDF。您能提供引发异常的文件名吗?文件名:Bills_080415.PDF。。。我使用相同的文件命名约定使用此代码处理了许多这样的文件。我查看了异常情况,似乎是在iTextSharp.text.pdf.PdfReader.ReadPages方法调用期间发生的。文件可能已损坏或格式不正确。请共享有问题的PDF。在Adobe Reader中打开的PDF不一定是有效的PDF。如果不共享PDF,则无法回答您的问题。路径中不存在可能导致不同堆栈跟踪的null。
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)