OP 的示例文件包含多个部分,如下所示:
OP 在评论中提到:
另一种工具完全按照我想要的方式解析我的 PDF。 [...]
PS:这个工具是pdfbox
在此方法中使用PDFBox(v1.8.10,当前发布版本):
String extract(PDDocument document) throws IOException
{
PDFTextStripper stripper = new PDFTextStripper();
return stripper.getText(document);
}
返回上面显示的部分
Driver Book for 8/5/2015
Company IS MEDICAL; AND Date of Service IS BETWEEN 08/05/2015 AND 08/05/2015; AND Status IS Assigned; AND Vehicles IS MEDICAL:
CATY
MEDICAL
Trip #: 314-A
Comments: ----LIVERY---
Destination:Pick-up:
Call Type: Livery
<Doctor Office>
REGO PARK, (631)
000-0000
(718) 896-5953
74- AVE 204E HEIGHTS, NY
11372 (718) 639-4154
11:00:00 PAT, MIKHAIL
Trip #: 314-B
Comments: ----LIVERY---
Destination:Pick-up:
Call Type: Livery
74- AVE 204E HEIGHTS, NY
11372 (718) 639-4154
<Doctor Office>
63-6 REGO PARK, NY
11374 (631) 000-0000
11:01:00 PAT, MIKHAIL
这并不是真正的整齐的按列提取,但某些信息块(如地址块)仍然保留在一起。
使用 iText(Sharp) 获得相同的输出实际上非常容易:只需显式使用SimpleTextExtractionStrategy
而不是LocationTextExtractionStrategy
这是默认使用的,即必须替换这一行
page = PdfTextExtractor.GetTextFromPage(reader, i);
by
page = PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy());
除了每个数据集有一个空格字符外(iText(Sharp) 提取Destination: Pick-up:
代替Destination:Pick-up:
)结果是相同的。
关于 PDFBox 提取文本的结论:
所以我认为 PDF 确实是表结构的。
实际上,这种提取顺序仅仅意味着在PDF页面内容流中绘制字符串段的操作按照这个顺序发生。由于根据 PDF 规范,这些操作的顺序是任意的,因此生成这些 PDF 的软件的任何更新都可能导致 PDFBox 无法使用这些文件PDFTextStripper
和 iTextSimpleTextExtractionStrategy
仅仅提取出一堆难以理解的字符。
PS:如果设置了PDFBoxPDFTextStripper
财产SortByPosition
to true
像这样
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true);
return stripper.getText(document);
然后 PDFBox 像 iText(Sharp) 一样使用(默认)提取文本LocationTextExtractionStrategy
does
OP 表示对内容流中固有的块结构感兴趣。最明显的结构(如通用 PDF 中的结构)是文本对象(其中可以绘制多个字符串)。
在手头的情况下SimpleTextExtractionStrategy
用来。它可以轻松扩展,以在其输出中包含与文本对象的开头和结尾相对应的标记。在 Java 中,这可以通过使用匿名类来完成,如下所示:
return PdfTextExtractor.getTextFromPage(reader, pageNo, new SimpleTextExtractionStrategy()
{
boolean empty = true;
@Override
public void beginTextBlock()
{
if (!empty)
appendTextChunk("<BLOCK>");
super.beginTextBlock();
}
@Override
public void endTextBlock()
{
if (!empty)
appendTextChunk("</BLOCK>\n");
super.endTextBlock();
}
@Override
public String getResultantText()
{
if (empty)
return super.getResultantText();
else
return "<BLOCK>" + super.getResultantText();
}
@Override
public void renderText(TextRenderInfo renderInfo)
{
empty = false;
super.renderText(renderInfo);
}
});
(文本提取.java https://github.com/mkl-public/testarea-itext5/blob/master/src/test/java/mkl/testarea/itext5/extract/TextExtraction.java method extractSimple
)
(这个 Java 代码应该很容易翻译成 C#。empty
布尔值可能看起来很有趣;不过,这是必要的,因为一旦将某些块附加到提取的内容,基类就假定要设置某些附加属性。)
使用这一扩展策略,我们可以得到上面所示的部分:
<BLOCK>Driver Book for 8/5/2015
Company IS MEDICAL; AND Date of Service IS BETWEEN 08/05/2015 AND 08/05/2015; AND Status IS Assigned; AND Vehicles IS MEDICAL:
CATY</BLOCK>
<BLOCK>
MEDICAL</BLOCK>
<BLOCK>
Trip #: 314-A</BLOCK>
<BLOCK>
Comments: ----LIVERY---</BLOCK>
<BLOCK>
Destination: Pick-up:</BLOCK>
<BLOCK>
Call Type: Livery
<Doctor Office>
REGO PARK, (631)
000-0000
(718) 896-5953</BLOCK>
<BLOCK>
74- AVE 204E HEIGHTS, NY
11372 (718) 639-4154</BLOCK>
<BLOCK>
11:00:00</BLOCK>
<BLOCK> PAT, MIKHAIL</BLOCK>
<BLOCK>
Trip #: 314-B</BLOCK>
<BLOCK>
Comments: ----LIVERY---</BLOCK>
<BLOCK>
Destination: Pick-up:</BLOCK>
<BLOCK>
Call Type: Livery
74- AVE 204E HEIGHTS, NY
11372 (718) 639-4154</BLOCK>
<BLOCK>
<Doctor Office>
63-6 REGO PARK, NY
11374 (631) 000-0000</BLOCK>
<BLOCK>
11:01:00</BLOCK>
<BLOCK> PAT, MIKHAIL</BLOCK>
由于这将地址保留在同一块中,因此这可能在提取过程中有所帮助。