改进多列 PDF 文本提取
Contents
[
Hide
]
多列布局通常需要额外处理以改进阅读顺序和提取质量。
在降低字体大小后提取文本
此技术会更新文本片段的字体大小,将调整后的文档保存到内存中,然后从转换后的结果中提取文本。
- 打开源 PDF 于 Document 实例。
- 创建一个 TextFragmentAbsorber 并访问所有文档页面以收集 TextFragment 对象。
- 遍历片段并按请求的比例缩小每个字体大小,以便在提取前可以对密集的列布局进行标准化。
- 保存已调整的 Document 到内存字节流中。
- 重新打开第二个 Document 来自该内存缓冲区。
- 创建一个 TextAbsorber,访问已转换文档的所有页面,并将提取的文本写入输出文件。
public static void extractTextReduceFont(Path inputFile, Path outputFile, double reduceRatio) throws Exception {
try (Document document = new Document(inputFile.toString())) {
TextFragmentAbsorber fragmentAbsorber = new TextFragmentAbsorber();
document.getPages().accept(fragmentAbsorber);
for (TextFragment fragment : fragmentAbsorber.getTextFragments()) {
fragment.getTextState().setFontSize((float) (fragment.getTextState().getFontSize() * reduceRatio));
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
document.save(stream);
try (Document document2 = new Document(new ByteArrayInputStream(stream.toByteArray()))) {
TextAbsorber textAbsorber = new TextAbsorber();
document2.getPages().accept(textAbsorber);
Files.writeString(outputFile, textAbsorber.getText());
}
}
}
使用比例因子提取文本
使用 TextExtractionOptions 在纯格式化模式下,调节列密集布局的比例因子。
- 打开源 PDF 于 Document 实例。
- 创建一个 TextAbsorber 用于完整文档提取。
- 创建 TextExtractionOptions 以纯格式模式进行,以使用布局敏感的提取行为。
- 在访问页面之前,设置比例因子并将提取选项应用于吸收器。
- 遍历所有文档页面并将提取的文本写入输出文件。
public static void extractTextScaleFactor(Path inputFile, Path outputFile, double scaleFactor) throws Exception {
try (Document document = new Document(inputFile.toString())) {
TextAbsorber textAbsorber = new TextAbsorber();
TextExtractionOptions extractionOptions =
new TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Pure);
extractionOptions.setScaleFactor(scaleFactor);
textAbsorber.setExtractionOptions(extractionOptions);
document.getPages().accept(textAbsorber);
Files.writeString(outputFile, textAbsorber.getText());
}
}