【发布时间】:2020-06-10 21:53:20
【问题描述】:
我正在尝试从多页 PDF 文档中提取文本,几乎所有文档都可以正常提取,但有几个文档因编码 10000 错误而崩溃。不工作的文档页面的唯一独特之处在于它们有一个按钮和表单域。
{
var pageNumbersToSave = new List<int>();
for (var i = 1; i <= r.NumberOfPages; i++)
{
try
{
var s = PdfTextExtractor.GetTextFromPage( r, i, new SimpleTextExtractionStrategy() );
我还尝试使用 PDFStamper 来展平表单元素,但这并没有改变任何东西:
byte[] flatBytes;
using ( var r = new PdfReader( pdfBytes ) )
{
using (var ms = new MemoryStream())
{
using (var flattener = new PdfStamper(r, ms))
{
for ( var i = 1; i <= r.NumberOfPages; i++ )
{
r.AcroFields.RemoveFieldsFromPage( i );
}
flattener.FormFlattening = true;
flattener.Close();
}
flatBytes = ms.ToArray();
}
}
如果我使用压模进行测试,显然在顶部代码中我使用的是 flatBytes 而不是 pdfBytes。
完整的异常消息: 没有可用于编码 10000 的数据。有关定义自定义编码的信息,请参阅 Encoding.RegisterProvider 方法的文档。
Stack Trace:
at System.Text.Encoding.GetEncoding(Int32 codepage)
at System.Text.Encoding.GetEncoding(Int32 codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
at iTextSharp.text.xml.simpleparser.IanaEncodings.GetEncodingEncoding(String name)
at iTextSharp.text.pdf.PdfEncodings.ConvertToString(Byte[] bytes, String encoding)
at iTextSharp.text.pdf.DocumentFont.FillEncoding(PdfName encoding)
at iTextSharp.text.pdf.DocumentFont.DoType1TT()
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.ProcessContent(Byte[] contentBytes, PdfDictionary resources)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.FormXObjectDoHandler.HandleXObject(PdfContentStreamProcessor processor, PdfStream stream, PdfIndirectReference refi, ICollection markedContentInfoStack)
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.ProcessContent(Byte[] contentBytes, PdfDictionary resources)
at iTextSharp.text.pdf.parser.PdfReaderContentParser.ProcessContent[E](Int32 pageNumber, E renderListener, IDictionary`2 additionalContentOperators)
at iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(PdfReader reader, Int32 pageNumber, ITextExtractionStrategy strategy)
at VerataParsers.ECWPdfExtractor.StripImagesFromPdf(Byte[] pdfBytes, Int32& adjustedPageCount) in C:\Users\Dell T5610\source\repos\SecureDirectMessaging\VerataParsers\ECWPdfExtractor.cs:line 85
【问题讨论】:
-
“但有几个文档因编码 10000 错误而崩溃” - 这是什么意思?此外,如果问题只出现在非常特殊的 pdf 中,我们显然需要一个示例 pdf 来分析问题...
-
这意味着在大约 3000 页中,有 2 页抛出该异常。由于 HIPAA,我无法上传示例。我 99% 确信这是由于违规页面上的表单元素,因为这是不良页面上的共性,其他一切正常。
-
如果没有示例,我们可能无法认真提供帮助。但是您可以从正确引用异常及其堆栈跟踪开始,可能基于该信息可以为您提供进一步调查的方向。
-
添加了异常消息和堆栈跟踪。感谢您的观看!
-
好的。因此,在出现问题的页面上有一个 XObject,其字体的编码值很麻烦。它不是一个活动的表单域,但它可能是一个扁平的表单域。因此,在使用 RUPS 或类似工具检查文档时,请检查那些字体编码条目。
标签: c# itext .net-core-3.1