【问题标题】:Differ between text above image, and text covered by image图像上方的文本和图像覆盖的文本之间的区别
【发布时间】:2021-10-25 05:17:57
【问题描述】:

我已经测试了in this thread 提供的代码。它可以找到包含在图像边界框中的所有文本元素。但是,您如何区分图片背后的文字和图片上方的文字呢?

【问题讨论】:

  • 你检查过this older answer吗?它为忽略位图图像覆盖的文本的文本提取提供了概念验证。
  • 是的,我必须移植它,将 context.processSubStream 替换为 context.showForm。不幸的是,它并没有带来预期的结果。也许我没有提到OperatorProcessor.getName() 的正确值。应该是什么价值?我知道您使用了"Do",但它对我不起作用。
  • 对不起,我重新检查了我的代码,做了一些清理工作,现在可以了!!!!谢谢@mkl!将更新的移植代码粘贴到下面的答案中。
  • “我不得不移植它” - 是的,考虑到那个答案的年代,它很可能是一个 PDFBox 1.x 的答案。 - “现在可以了!!!!” - 太好了!
  • @mkl 该代码适用于带有pdPage.getCropBox().getLowerLeftY() == 0getLowerLeftX() == 0 的PDF 页面。对此old thread comment 的任何帮助将不胜感激:For a generic solution you have to change this test to something that checks whether the 1x1 square transformed by the Matrix ctm = getGraphicsState().getCurrentTransformationMatrix() overlaps the character box ...

标签: java pdfbox


【解决方案1】:

下面粘贴的是old answer mentioned above的代码,移植到PDFBox 2.0.24。主要变化是:

  • 已添加getName() 方法
  • context.processSubStream 替换为 context.showForm
  • PDXObjectFormPDXObjectImage 替换为新的类名PDFormXObjectPDImageXObject
  • drawer.getResources().getXObjects(); 替换为 drawer.getResources().getXObjectNames() 并且对 XObjects 集合的迭代基于 getXObjectNames() 返回值。
public final class CoveredText extends OperatorProcessor
{
    @Override
    public void process(Operator operator, List<COSBase> operands) throws IOException{
        PDFVisibleTextStripper drawer = (PDFVisibleTextStripper)context;
        for (COSName objectName: drawer.getResources().getXObjectNames()) {
            PDXObject xobject = drawer.getResources().getXObject(objectName);
            if ( xobject == null )
            {
                System.out.println("CoveredText.process Can't find the XObject for '"+objectName.getName()+"'");
            }
            else if( xobject instanceof PDImageXObject )
            {
                System.out.println("CoveredText.process " + objectName.getName()+" is a PDImageXObject");
                drawer.hide(objectName.getName());
            }
            else if(xobject instanceof PDFormXObject)
            {                   
                PDFormXObject form = (PDFormXObject)xobject;
                System.out.println("CoveredText.process " + objectName.getName()+" is a PDFormXObject at localtion " + form.getBBox().toString());
                Matrix matrix = form.getMatrix();
                if (matrix != null) 
                {
                    Matrix xobjectCTM = matrix.multiply( context.getGraphicsState().getCurrentTransformationMatrix());
                    context.getGraphicsState().setCurrentTransformationMatrix(xobjectCTM);
                }
                context.showForm(form);                    
            }               
        }
    } 
    @Override
    public String getName() {
        return "Do";
    }
}

【讨论】:

  • 你能解释一下你改变了什么吗?问题出在哪里以及如何解决?
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 此代码适用于带有pdPage.getCropBox().getLowerLeftY() == 0getLowerLeftX() == 0 的PDF 页面。对这个旧线程评论的任何帮助都会非常感激:For a generic solution you have to change this test to something that checks whether the 1x1 square transformed by the Matrix ctm = getGraphicsState().getCurrentTransformationMatrix() overlaps the character box ...
  • 定位问题通过比较 TextPosition 和图像边界框来解决,两者都以用户空间为单位。我对以下页面还有另一个问题:drive.google.com/file/d/14qy_GPS3dzXI-meJiCKkvqwUb59Q1yWk/… 我无法获取打印在右上角图像后面的字符串“ANNUAL REPORT 2018”,被检测为隐藏(= 覆盖),以及字符串“Destination2050”被检测为可见 = 在图像顶部。有什么帮助吗?
  • 我已从我的云文件夹中删除了 PDF 样本。可在此处获得:s25.q4cdn.com/680186029/files/doc_financials/ar-interactive/… 问题在第 14 页(基于零的计数)
猜你喜欢
  • 2018-02-07
  • 1970-01-01
  • 2020-08-24
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
相关资源
最近更新 更多