我有下面的代码
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。我能读懂1000秒的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
发布于 2015-11-06 17:05:07
为了结束这个话题,我已经要求PDF供应商为我重新生成相关的文件。它们确实重新生成并发送给我,我可以在没有任何代码更改的情况下处理它们。看来,iTextSharp无法正确读取的PDF内容中有一些错误。我仍然感到奇怪,因为他们的进程和我们的进程没有任何变化。可能是某个地方损坏的PDF。
发布于 2015-11-02 21:37:40
最简单的原因是,在这4个PDF上,pdfPath是空的,而不是字符串。检查pdfPath中的空值。
发布于 2015-11-05 21:01:55
4 PDF的路径可能无效,这意味着那里没有PDF文件。
https://stackoverflow.com/questions/33485665
复制相似问题