首页 > 解决方案 > iText7 (PdfSweep):System.ArgumentException:CharacterRenderInfo 对象表示单个字符

问题描述

尝试使用 PDFSweep 2.0 (C#) 编辑 PDF 中的一些文本。以下代码适用于我拥有的大多数 PDF 文件,但在某些 PDF 文件上,下面的代码会引发 ArgumentException

String input = "G:/tmp/redact/input.pdf";
String output = "G:/tmp/redact/output.pdf";
CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
strategy.Add(new RegexBasedCleanupStrategy(@"Hello"));

PdfDocument pdf = new PdfDocument(new PdfReader(input), new PdfWriter(output));
PdfAutoSweep autoSweep = new PdfAutoSweep(strategy);
autoSweep.CleanUp(pdf);           
pdf.Close();

完整的异常堆栈如下所示:

Unhandled Exception: System.ArgumentException: CharacterRenderInfo objects represent a single character. They should not be made from TextRenderInfo objects containing more than a single character of text.
   at iText.Kernel.Pdf.Canvas.Parser.Listener.CharacterRenderInfo..ctor(TextRenderInfo tri)
   at iText.Kernel.Pdf.Canvas.Parser.Listener.RegexBasedLocationExtractionStrategy.ToCRI(TextRenderInfo tri)
   at iText.Kernel.Pdf.Canvas.Parser.Listener.RegexBasedLocationExtractionStrategy.EventOccurred(IEventData data, EventType type)
   at iText.PdfCleanup.Autosweep.CompositeCleanupStrategy.EventOccurred(IEventData data, EventType type)
   at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.EventOccurred(IEventData data, EventType type)
   at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.DisplayPdfString(PdfString string)
   at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.ShowTextOperator.Invoke(PdfCanvasProcessor processor, PdfLiteral operator, IList`1 operands)
   at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.InvokeOperator(PdfLiteral operator, IList`1 operands)
   at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.ProcessContent(Byte[] contentBytes, PdfResources resources)
   at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.ProcessPageContent(PdfPage page)
   at iText.Kernel.Pdf.Canvas.Parser.PdfDocumentContentParser.ProcessContent[E](Int32 pageNumber, E renderListener, IDictionary`2 additionalContentOperators)
   at iText.Kernel.Pdf.Canvas.Parser.PdfDocumentContentParser.ProcessContent[E](Int32 pageNumber, E renderListener)
   at iText.PdfCleanup.Autosweep.PdfAutoSweep.GetPdfCleanUpLocations(PdfDocument doc)
   at iText.PdfCleanup.Autosweep.PdfAutoSweep.CleanUp(PdfDocument pdfDocument)
   at gp_redactor.Program.Main(String[] args) in G:\prj\gp_redactor\gp_redactor\Program.cs:line 70

我倾向于认为这是 iText 中的一个错误,任何人都可以提出解决方法吗?

标签: c#itext7

解决方案


推荐阅读