我想从 pdf 中提取特定文本我有文本的确切位置
我尝试使用 itext7 进行提取,但是当我为提取创建具有正确尺寸的矩形时,它似乎太大而无法匹配文本,但尺寸是正确的,我尝试了 SimpleTextExtractionStrategy 和 LocationTextExtractionStrategy 相同的结果pdfFile
private void estraiValori(PdfPage page) {
for (Entry<String, Elemento> entry : templateMap.entrySet()) {
String key = entry.getKey();
Elemento value=(Elemento) entry.getValue();
//Rectangle tmp=new Rectangle((float)238.64,(float) 14.8,(float) 122,(float) 28.7);
TextRegionEventFilter fontFilter = new TextRegionEventFilter(value.getDim()); //getDim is a rectangle
FilteredEventListener listener = new FilteredEventListener();
//LocationTextExtractionStrategy extractionStrategy = listener.attachEventListener(new LocationTextExtractionStrategy(), fontFilter);
SimpleTextExtractionStrategy extractionStrategy = listener.attachEventListener(new SimpleTextExtractionStrategy(), fontFilter);
new PdfCanvasProcessor(listener).processPageContent(page);//page is a PdfPage
String actualText = extractionStrategy.getResultantText();
System.out.println(actualText);
}
}
有多种方式可以在 PDF 中显示(视觉上)相同的内容。您可以逐字形或在整个句子中附加文本字形。TextRegionEventFilter
在过滤之前不会将较大的文本块拆分为较小的文本块。如果文本被写成一大块,而您只想要其中的一部分,则需要对原始内容进行预处理,即拆分成更小的块。
幸运的是,iText 提供了一种开箱即用的方式来做到这一点 - 类被调用GlyphTextEventListener
并且可以链接到其他ITextExtractionStrategy
实例。只需ITextExtractionStrategy
按以下方式将您的听众包装起来:
TextRegionEventFilter filter = new TextRegionEventFilter(new Rectangle(x1, y1, x2, y2));
ITextExtractionStrategy filteredListener = new FilteredTextEventListener(new LocationTextExtractionStrategy(), filter);
ITextExtractionStrategy fineGrainedListener = new GlyphTextEventListener(filteredListener);
new PdfCanvasProcessor(fineGrainedListener).processPageContent(page);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句