使用 iTextSharp(PDF 到文本)修复错误?

Fix the error with iTextSharp (PDF to text)?

我正在尝试通过以下方式使用 iTextSharp (NuGet) 从 PDF 文件中恢复文本:

this.Cursor = Cursors.WaitCursor;
string LOC_DOC = @"C:\PDF_files";

string[] PDFs = Directory.GetFiles(LOC_DOC, "*.pdf", SearchOption.AllDirectories);

    foreach (string PDF in PDFs)
    {

         PdfReader reader = new PdfReader(@PDF);
             
         for (int page = 1; page <= reader.NumberOfPages; page++)
         {
             string pageText = PdfTextExtractor.GetTextFromPage(reader, page);
         }

    }

 this.Cursor = Cursors.Default;

目标只是检索PDF的内容,稍后我会做处理。 它适用于某些 PDF,但对于其他 PDF,我遇到以下错误:

System.InvalidCastException : 'Impossible d'effectuer un cast d'un objet de type 'iTextSharp.text.pdf.PdfLiteral' en type 'iTextSharp.text.pdf.PdfNumber'.'

System.InvalidCastException : 'Impossible to cast an objet of type 'iTextSharp.text.pdf.PdfLiteral' to type 'iTextSharp.text.pdf.PdfNumber'.'

有人有想法吗?

分析

您共享的 PDF,2-30-SL-manual-DE.pdf,在第 6 页的内容流中有一个错误导致了这种情况下的异常:

0.1 -16 TD -3.796 Tw
[ (.)2.943 Tw ( . . . . . . . . . . . . . . .)] TJ
-0.138 -16 TD -3.796 Tw
[ (.)2.943 Tw ( . . . . . . . . . . . . . . .)] TJ
0.112 -16 TD -3.45 Tw
[ (.)3.05 Tw ( . . . . . . . . . .)] TJ 

Tw指令的各个数组参数中的Tws是无效的,该数组只能包含字符串(在圆形或尖括号)和数字,参见规范:

array TJ Show one or more text strings, allowing individual glyph positioning. Each element of array shall be either a string or a number. If the element is a string, this operator shall show the string. If it is a number, the operator shall adjust the text position by that amount.

(ISO 32000-1, Table 109 – Text-showing 操作员)

如果您的其他文档导致相同的异常(包括类似的堆栈跟踪),它们很可能在某些 TJ[=42 中包含此类无效的 non-string、non-number 文字=] 也有说明。

因此,要求文件来源提供这些文件的固定副本。

Work-around

在您提到的评论中

However these are old documents, it is impossible for me to request a corrected version

如果示例文件中发现的内容流错误是您文件中唯一的内容错误类型,并且忽略额外文字始终是处理错误的适当方法,您可以将 IContentOperator 在另一个运算符中处理 TJ iText 文本提取中的指令,该运算符从参数中过滤不需要的文字:

PdfReader reader = new PdfReader(@"d:\Issues\Whosebug\Fix the error with iTextSharp (PDF to text)-30-SL-manual-DE.pdf");
for (int page = 1; page <= reader.NumberOfPages; page++)
{
    Console.Out.WriteLine("Page {0}", page);
    PdfDictionary pageDic = reader.GetPageN(page);
    PdfDictionary resourcesDic = pageDic.GetAsDict(PdfName.RESOURCES);
    ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();
    PdfContentStreamProcessor processor = new PdfContentStreamProcessor(strategy);
    TjArgumentClearingWrapper wrapper = new TjArgumentClearingWrapper();
    wrapper.WrappedOperator = processor.RegisterContentOperator("TJ", wrapper);
    processor.ProcessContent(ContentByteUtils.GetContentBytesForPage(reader, page), resourcesDic);
    string pageText = strategy.GetResultantText();
    Console.Out.WriteLine("******\n{0}\n\n", pageText);
}

与帮手 class

class TjArgumentClearingWrapper : IContentOperator
{
    public void Invoke(PdfContentStreamProcessor processor, PdfLiteral oper, List<PdfObject> operands)
    {
        PdfObject operand = operands[0];
        if (operand is PdfArray array)
        {
            PdfArray newArray = new PdfArray();
            foreach (PdfObject pdfObject in array)
            {
                if (pdfObject is PdfString || pdfObject is PdfNumber)
                    newArray.Add(pdfObject);
            }
            operands[0] = newArray;
        }
        WrappedOperator.Invoke(processor, oper, operands);
    }

    public IContentOperator WrappedOperator { get; set; }
}