【问题标题】:Extracting text with iTextSharp throws an InvalidCastException使用 iTextSharp 提取文本会引发 InvalidCastException
【发布时间】: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


【解决方案1】:

相关文档包含具有以下 ToUnicode 映射的字体:

/CIDInit /ProcSet findresource
begin
12 dict
begin
/CIDSystemInfo <</Ordering (UCS) /Registry (Adobe) /Supplement 0 >> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <ffffffffffffffff> endcodespacerange
20 beginbfchar
<0003> <0020> <0012> <0043> <0018> <0044> <0045> <004e> <0059> <0051> <005e> <0053> <0102> <0061> <0110> <0063> <011a> <0064> <011e> <0065> <015d> <0069> <0175> <006d> <0176> <006e> <017d> <006f> <01ffffff89> <0070> <01ffffff8c> <0072> <01ffffff90> <0073> <01ffffff9a> <0074> <01ffffffb5> <0075> <01ffffffc7> <0079> endbfchar
100 beginbfchar
<01ffffffcc> <007a> endcmap
CMapName
currentdict
/CMap defineresource
pop
end
end
ý

iText(Sharp) 出错的部分是:

100 beginbfchar
<01ffffffcc> <007a> endcmap

即以beginbfchar 开头并以不匹配的endcmap 结尾的部分。

我认为以beginbfchar 开头的部分总是必须以endbfchar 结尾。

有问题的字体是 Calibri 子集复合字体。它以 xobject 的形式使用,用作第一页上的 Fm0。该 xobject 有一个字典条目

/PTEX.FileName (C:/MyFiles/Publications/DisparityMetric/Figures/Teaser.pdf)

所以它可能是从 Teaser.pdf 文件中复制的。

【讨论】:

  • 我会对此进行调查并报告。谢谢你的信息。
  • 你是对的,Calibri 字体产生了这个错误。再次感谢。现在出于好奇,这是 iText 或 Calibri 字体中的一个小错误 :-) 吗?
  • 我认为部分的开始和结束必须在这里匹配。所以嵌入的子集字体是问题所在。很可能问题不在原始字体中,而是由子集生成器引入。
猜你喜欢
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多