【问题标题】:iText: Extracted text from pdf file using LocationTextExtractionStrategy is in wrong orderiText:使用 LocationTextExtractionStrategy 从 pdf 文件中提取的文本顺序错误
【发布时间】:2016-05-22 13:30:16
【问题描述】:

我正在使用 iText 从特定位置的 pdf 文件中提取一些文本。 为此,我正在使用 LocationTextExtractionStrategy:

public static void main(String[] args) throws Exception {

    PdfReader pdfReader = new PdfReader("location_text_extraction_test.pdf");

    Rectangle rectangle = new Rectangle(38, 0, 516, 516);

    RenderFilter[] filter = {new RegionTextRenderFilter(rectangle)};
    TextExtractionStrategy strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
    String text = PdfTextExtractor.getTextFromPage(pdfReader, 1, strategy);

    System.out.println(text);

    pdfReader.close();
}

Link to pdf file

问题是提取的文本顺序错误:

应该提取为:

Part Description Quantity Unit Price Total For Line Extended Price
Landing Fee 1.00 407.84 $ USD 407.84 407.84 $

提取为:

Total For Line Extended Price
Part Description Quantity Unit Price
1.00 407.84 $ USD 407.84 407.84 $
Landing Fee

请注意,当我在 Acrobat 中打开 pdf 时,使用 Ctrl+A 选择所有文本,复制然后将其粘贴到文本编辑器中,所有内容都按正确的顺序排列。

有没有办法解决这个问题? 非常感谢;)

【问题讨论】:

  • 最有可能“总行扩展价格”略高于“零件描述数量单价”,因此,之前提取。
  • 答案有帮助吗?如果不是,请指出问题。
  • 抱歉,我很忙,无法测试您的答案中提供的解决方案。我会尽快找到一些时间;)

标签: pdf itext text-extraction


【解决方案1】:

造成这种情况的原因很简单,“总价格”位于 y 坐标 507.37,而“零件描述数量单价”位于 y 506.42 的坐标。

LocationTextExtractionStrategy 允许通过仅考虑 y 坐标的整数部分来进行微小的变化,但即使是整数部分也有所不同。因此,它假定前一个标题在后一个标题的上方,并相应地输出其结果。

如果出现此类变化,通常第一次尝试可能是尝试SimpleTextExtractionStrategy。不幸的是,这在这里没有帮助,因为前一个文本实际上是在后一个文本之前绘制的。因此,此策略也会以错误的顺序返回标题。

在这种情况下,您需要一种不同的策略,例如来自this answer的策略HorizontalTextExtractionStrategyHorizontalTextExtractionStrategy2(取决于你的iText版本,前一个最高为iText 5.5.8,后一个为当前开发代码5.5.9-SNAPSHOT)。使用它你会得到

Part Description Quantity Unit Price Total For Line Extended Price
Landing Fee 1.00 407.84 $ USD 407.84 407.84 $
Parking 1.00 101.96$ USD 101.96 101.96$
??? 1.00 51.65$ USD 51.65 51.65$
Pax Baggage Handling Fee 5.00 8.49$ USD 42.45 42.45 $
Pax Airport Tax 5.00 26.36 $ USD 131.80 131.80$
GA terminal for crew on Arr ferry fit 1.00 125.00$ USD 125.00 125.00$
VIP lounge for Pax on Dep. 5.00 124.00$ USD 620.00 620.00 $
GA terminal for crew on dep. 1.00 125.00$ USD 125.00 125.00$
VIP lounge for Guest on Dep. 1.00 38.00$ USD 38.00 38.00 $
Crew transfer on arr 1.00 70.00 $ USD 70.00 70.00 $
Crew transfer on dep 1.00 70.00 $ USD 70.00 70.00 $
Lavatory Service 1.00 75.00 $ USD 75.00 75.00 $
Catering-ISS 1.00 1,324.28 $ USD 1,324.28 1,324.28 $
Ground Handling 1.00 190.00$ USD 190.00 190.00$
Pax Handling 1.00 190.00$ USD 190.00 190.00$
Push Back 1.00 83.00 $ USD 83.00 83.00 $
Towing 1.00 110.00$ USD 110.00 110.00$

(使用TextExtraction测试方法testLocation_text_extraction_test的结果)

不幸的是,如果在不同的并排列中有重叠的线,这些策略就会失败,例如在您的文档中包含发票收件人地址及其右侧的信息。

您可以尝试调整水平策略(例如,通过分析分隔列的水平间隙)或尝试组合方法,使用同一文档的多个策略的输出。

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 2012-11-12
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多