我有一个小PDF文档,这是一个裁剪版本从一个更大的PDF文档。我惊讶地看到,itext5 (使用自定义位置策略)仍然在复制裁剪后遗漏的所有文本。所有这些文本都不能通过Acrobat阅读器看到。
我如何让它5检测和忽略这样的隐藏文本?链接到带有隐藏文本的PDF
编辑1错误文档被超链接
编辑2 -附代码段
public class MyLocationTextExtractionStrategy :
LocationTextExtractionStrategy
{
public void RenderText(TextRenderInfo renderInfo)
{
string text = renderInfo.GetText();
}
}谢谢你,索拉布
发布于 2018-05-15 11:27:04
从…的结合
我惊讶地看到,itext5 (使用自定义位置策略)仍然在复制裁剪后遗漏的所有文本。
以及你的代码片段
公共类MyLocationTextExtractionStrategy : LocationTextExtractionStrategy { public void RenderText(TextRenderInfo renderInfo) { string text = renderInfo.GetText();}
我假设您实际上感到惊讶的是,在您的RenderText方法的MyLocationTextExtractionStrategy中,您检索了超出裁剪框的文本TextRenderInfo对象。
但这是很自然的!您的RenderText方法实现了IRenderListener接口的方法,并且对页面内容中的每个匹配的绘图指令调用该接口的方法,不管它们的结果最终是否是可见的。
我怎样才能让它5检测和忽略这样的隐藏文本?
通过根据当前文档页的裁剪框的坐标检查文本坐标,可以很容易地检测和忽略裁剪框外的文本。
iText实际上包含一个过滤器架构,它允许排除文本块以达到不满足某些标准的策略。
如果你现在就像这样使用你的策略:
MyLocationTextExtractionStrategy strategy = new MyLocationTextExtractionStrategy();
PdfTextExtractor.GetTextFromPage(pdfReader, 1, strategy);您可以像这样应用麦田框区域筛选器:
MyLocationTextExtractionStrategy strategy = new MyLocationTextExtractionStrategy();
FilteredTextRenderListener strategyWithFilter = new FilteredTextRenderListener(strategy,
new RegionTextRenderFilter(pdfReader.GetCropBox(1)));
PdfTextExtractor.GetTextFromPage(pdfReader, 1, strategyWithFilter);作为旁白:
我要文档中所有可见的文本。当我说“可见”时,我指的是通过可见的文本。我不想将文本限制在任何具体领域。所有可见的文字。
- it may be drawn in the same color as the background, e.g. white on white,
- some setting or operation may transform text color and background color to the same color even though they may differ originally,
- a text rendering mode may be used that doesn't draw anything to start with,
- the glyphs of the font used for the text may be invisible,
- the text may be covered by some image,
- ...文本提取将提取所有那些“看不见的”文本片段。
(在一定程度上,您可以扩展文本提取框架以识别这一点,在堆栈溢出的情况下,您可以在这里找到关于此类扩展的许多问题和答案,但总会有一些情况您没有涵盖。)
如果这些问题是您的节目停止,文本提取是错误的技术,您应该使用OCR代替。
https://stackoverflow.com/questions/50330362
复制相似问题