【发布时间】:2014-09-24 06:44:53
【问题描述】:
我目前正在使用 iTextSharp 从 PDF 文件中提取文本。
对于几十个 PDF,它可以正常工作,但是其中 2 个 PDF 在 [1] 处引发了无效的强制转换异常 Stacktrace。
引发此异常的代码如下(GetTextFromPage 处引发异常):
PdfReader reader = new PdfReader(byteArray);
PdfTextExtractor.GetTextFromPage(reader, 1, new SimpleTextExtractionStrategy());
一些补充说明:
- Adobe Acrobat 中的预检语法检查未发现任何错误。
- 产生此错误的示例 PDF 位于:http://resources.mpi-inf.mpg.de/DisparityModel(论文 (Adobe Acrobat PDF, 6.69 MB)。)
- 我已经尝试过 LocationTextExtractionStrategy - 同样的错误。
如果 PDF 文件损坏,我如何在 Preflight 旁边检查它?或者这个错误是从哪里来的?
[1]
System.InvalidCastException was unhandled
HResult=-2147467262
Message=Unable to cast object of type 'iTextSharp.text.pdf.PdfLiteral' to type 'iTextSharp.text.pdf.PdfString'.
Source=itextsharp
StackTrace:
at iTextSharp.text.pdf.DocumentFont.FillMetrics(Byte[] touni, IntHashtable widths, Int32 dw)
at iTextSharp.text.pdf.DocumentFont.ProcessType0(PdfDictionary font)
at iTextSharp.text.pdf.DocumentFont.Init()
at iTextSharp.text.pdf.DocumentFont..ctor(PRIndirectReference refFont)
at iTextSharp.text.pdf.CMapAwareDocumentFont..ctor(PRIndirectReference refFont)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.GetFont(PRIndirectReference ind)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.SetTextFont.Invoke(PdfContentStreamProcessor processor, PdfLiteral oper, List`1 operands)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.InvokeOperator(PdfLiteral oper, List`1 operands)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.ProcessContent(Byte[] contentBytes, PdfDictionary resources)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.FormXObjectDoHandler.HandleXObject(PdfContentStreamProcessor processor, PdfStream stream, PdfIndirectReference refi)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.DisplayXObject(PdfName xobjectName)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.Do.Invoke(PdfContentStreamProcessor processor, PdfLiteral oper, List`1 operands)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.InvokeOperator(PdfLiteral oper, List`1 operands)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.ProcessContent(Byte[] contentBytes, PdfDictionary resources)
at iTextSharp.text.pdf.parser.PdfReaderContentParser.ProcessContent[E](Int32 pageNumber, E renderListener)
at iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(PdfReader reader, Int32 pageNumber, ITextExtractionStrategy strategy)
at ConsoleApplication1.Program.Main(String[] args) in e:\foobar\projects\AnalyzePDF\ConsoleApplication1\ConsoleApplication1\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
【问题讨论】:
-
如果您无法共享 PDF,我们将无法重现该错误。如果我们不能重现错误,我们就无法调查。您使用的是哪个版本的 iTextSharp(最新的是 5.5.3)。你是客户吗?如果是这样,我们有 NDA 来处理敏感文件。
-
我正在使用最新的 iTextSharp 是的。不,我不是客户。您可以访问此站点上的 PDF 文件吗:resources.mpi-inf.mpg.de/DisparityModel(论文 (Adobe Acrobat PDF, 6.69 MB)。)?
-
不是马上。我仍在处理我的邮件,我有几个会议。我可能要等到 JavaOne 之后才有时间看这个。
-
iText 解析语法以查找 特定 PDF 对象(数字、名称、字符串、字典、数组...)。当它检测到不符合规范中定义的任何 PDF 对象的内容时,它会创建一个
PdfLiteral对象。在您的情况下,您的 PDF 有一个无法识别的对象,其中需要PdfString。其他工具会忽略此类对象。 iText 引发异常。或者这就是我的假设(我还没有看过 PDF)。 -
打印实际上并不重要。它在阅读第一页时已经崩溃。我已经相应地更新了问题中的代码。
标签: pdf itextsharp