【问题标题】:How to extract text from PDF according to its location?如何根据其位置从PDF中提取文本?
【发布时间】:2014-04-20 09:34:27
【问题描述】:

我有多个 PDF,我想从它们的第一页中提取某个区域的文本。 因此,鉴于我有 PDF 中文本边界框的坐标,如何使用命令行提取该文本。

我研究了一下,发现 PDFMiner 和 PDFBox 可以做到这一点。但是 PDFMiner 的文档记录很差。

谁能告诉我如何使用 PDFMiner 做到这一点?或者您是否可以提出其他解决方案?

PS:我在 Linux 终端上。

【问题讨论】:

    标签: linux pdf pdfminer


    【解决方案1】:

    您可以使用 PDFBox。 https://pdfbox.apache.org/apidocs/org/apache/pdfbox/util/PDFTextStripperByArea.html

    PDFTextStripperByArea stripper = new PDFTextStripperByArea();
    stripper.setSortByPosition( true );
    List allPages = document.getDocumentCatalog().getAllPages();
    PDPage firstPage = (PDPage)allPages.get( 0 );
    stripper.extractRegions( firstPage );
    stripper.addRegion( "class1", rectangle );
    System.out.println( "Text in the area:" + rectangle );
    System.out.println( "Text: " + stripper.getTextForRegion( "class1" ) );
    

    这里的rectange是java.awt包的Rectangle类的对象。 http://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html

    Rectangle rectange = new Rectangle(int x, int y, int width, int height);
    

    【讨论】:

      【解决方案2】:

      pdftotext(采用基于 Poppler 的最新版本之一)确实允许您定义一个页面区域以从中提取文本。

      试试这个:

      pdftotext    \
        -f 5       \
        -l 7       \
        -x 200     \
        -y 700     \
        -W 144     \
        -H 80      \
         input.pdf \
         output.txt
      

      它选择页面范围 5-7,以及一个宽度 = 144 点 (72 points == 1 inch)、高度 = 80 点的矩形,其中左上角位于 x 坐标 200 和 y 坐标 700。

      【讨论】:

      • 哇!智能解决方案。我从没想过使用pdftotext。这完美地工作。谢谢。
      • 我已经赞成这个答案,也接受了这个答案。也许刷新你的页面或其他东西。
      • 是的。我在没有实际测试的情况下对其进行了投票。经过测试,我终于接受了。
      猜你喜欢
      • 2011-09-30
      • 2015-03-19
      • 1970-01-01
      • 2023-01-18
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2023-04-02
      相关资源
      最近更新 更多