[英]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損壞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.