iText:使用 LocationTextExtractionStrategy 从 pdf 文件中提取的文本顺序错误
iText: Extracted text from pdf file using LocationTextExtractionStrategy is in wrong order
我正在使用 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();
}
问题是提取的文本顺序错误:
应提取的内容为:
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 时,select 使用 Ctrl+A 复制所有文本,然后将其粘贴到文本编辑器中,所有内容都按正确的顺序排列。
有办法解决这个问题吗?
非常感谢 ;)
原因很简单,"Total For Line Extended Price" 位于 y 坐标 507.37 而 "Part Description Quantity Unit Price" 位于 y 坐标 506.42.
LocationTextExtractionStrategy
允许通过仅考虑 y 坐标的整数部分进行小的变化,但此处的整数部分也不同。因此,它假定前一个标题位于后一个标题之上的一行上,并相应地输出其结果。
如果出现此类变化,通常首先尝试 SimpleTextExtractionStrategy
。不幸的是,这在这里没有帮助,因为前一个文本实际上是在后一个文本之前绘制的。因此,此策略还 returns 标题顺序错误。
在这种情况下,您需要一种不同的策略,例如策略 HorizontalTextExtractionStrategy or HorizontalTextExtractionStrategy2 (depending on your iText version, the former one up to iText 5.5.8, the latter one for the current development code 5.5.9-SNAPSHOT) from this answer。使用它你会得到
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
的结果)
不幸的是,如果不同的 side-by-side 列中有重叠线,这些策略就会失败,例如在您的文件中,发票收件人地址及其右侧的信息。
您可以尝试调整水平策略(例如,通过同时分析分隔列的水平间隙)或尝试组合方法,对同一文档使用多种策略的输出。
我正在使用 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();
}
问题是提取的文本顺序错误:
应提取的内容为:
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 时,select 使用 Ctrl+A 复制所有文本,然后将其粘贴到文本编辑器中,所有内容都按正确的顺序排列。
有办法解决这个问题吗? 非常感谢 ;)
原因很简单,"Total For Line Extended Price" 位于 y 坐标 507.37 而 "Part Description Quantity Unit Price" 位于 y 坐标 506.42.
LocationTextExtractionStrategy
允许通过仅考虑 y 坐标的整数部分进行小的变化,但此处的整数部分也不同。因此,它假定前一个标题位于后一个标题之上的一行上,并相应地输出其结果。
如果出现此类变化,通常首先尝试 SimpleTextExtractionStrategy
。不幸的是,这在这里没有帮助,因为前一个文本实际上是在后一个文本之前绘制的。因此,此策略还 returns 标题顺序错误。
在这种情况下,您需要一种不同的策略,例如策略 HorizontalTextExtractionStrategy or HorizontalTextExtractionStrategy2 (depending on your iText version, the former one up to iText 5.5.8, the latter one for the current development code 5.5.9-SNAPSHOT) from this answer。使用它你会得到
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
的结果)
不幸的是,如果不同的 side-by-side 列中有重叠线,这些策略就会失败,例如在您的文件中,发票收件人地址及其右侧的信息。
您可以尝试调整水平策略(例如,通过同时分析分隔列的水平间隙)或尝试组合方法,对同一文档使用多种策略的输出。