iTextSharp PDF 读取错误
iTextSharp PDF Read Error
我有以下代码
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
最简单的原因是,在这 4 个 PDF 中,pdfPath 为 null 而不是字符串。检查 pdfPath 中的空值。
4个PDF的路径可能是无效的,也就是说,那里没有PDF文件。
为了结束这个话题,我已经请求 PDF 供应商为我重新生成有问题的文件。他们确实重新生成并发送给我,我能够在不更改任何代码的情况下处理它们。 PDF 内容似乎有问题,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
最简单的原因是,在这 4 个 PDF 中,pdfPath 为 null 而不是字符串。检查 pdfPath 中的空值。
4个PDF的路径可能是无效的,也就是说,那里没有PDF文件。
为了结束这个话题,我已经请求 PDF 供应商为我重新生成有问题的文件。他们确实重新生成并发送给我,我能够在不更改任何代码的情况下处理它们。 PDF 内容似乎有问题,iTextSharp 无法正确读取。我仍然想知道,因为他们的流程和我们的流程都没有变化。它可能是某处损坏的 PDF。