2015-11-02 135 views
0

我有下面的代码iTextSharp的PDF阅读错误

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)) 

我不知道为什么它失败了几个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

+0

你能否提供引发异常的文件名? –

+0

文件名:Bills_080415.pdf ...我有很多这样的文件使用相同的文件命名约定使用此代码进行处理。 – techspider

+0

我看了异常,我似乎发生在'iTextSharp.text.pdf.PdfReader.ReadPages'方法调用期间。也许文件损坏或格式不正确。 –

回答

0

只是为了关闭这个主题,我已经要求PDF供应商为我重新生成有问题的文件。他们重新生成并发送给我,我可以在不更改任何代码的情况下处理它们。看起来,PDF内容中有些东西不能正确读取iTextSharp。我仍然怀疑,因为他们的流程和流程没有变化。它可能是某处损坏的PDF。

0

最简单的理由是,关于这4个PDF文件,pdfPath是空的,而不是一个字符串。检查pdfPath中的空值。

+0

路径中没有null – techspider

0

4个PDF文件的路径可能无效,也就是说,那里没有PDF文件。

+0

这会导致不同的堆栈跟踪。 – mkl