我试图从多页PDF文件中的每个页面的特定位置提取一段文本。当我使用iText从PDF文件中提取文本时我从前面的页面获取值
我有文本的位置,我能够在第一页上正确提取它。 但是在第一页之后的页面上,提取的文本似乎在积累。
例如,如果页面1上的文本值是“A”,页面2是“B”,页面3是“C”,那么我在我的输出字符串中通过FOR循环进行每次迭代接收以下值:
循环1:输出= A
环2:输出= BA
循环3:输出= CBA
我在我的p使用iTextSharp的项目,用C#编写。
任何帮助,将不胜感激。
var reader = new PdfReader(foregroundFile);
RectangleJ customerIdRectangle = new RectangleJ(0, 495, 108, 27);
RenderFilter[] filters = new RenderFilter[1];
LocationTextExtractionStrategy regionFilter = new LocationTextExtractionStrategy();
filters[0] = new RegionTextRenderFilter(customerIdRectangle);
FilteredTextRenderListener strategy = new FilteredTextRenderListener(regionFilter, filters);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
string output = "";
output = PdfTextExtractor.GetTextFromPage(reader, i, strategy);
Console.WriteLine(output);
}
我有同样的问题。阅读Bruno Lowagie给出的答案后,当我每次阅读页面时重新初始化LocationTextExtractionStrategy而不是重复使用同一个页面时,问题就消失了。 LocationTextExtractionStrategy ltes = new LocationTextExtractionStrategy(); string thePage = PdfTextExtractor.GetTextFromPage(_pdf_Reader,pageNumber,ltes); – JRB