gpt4 book ai didi

java - 按顺序阅读 PDFBox 中的段落

转载 作者:行者123 更新时间:2023-11-30 05:24:47 26 4
gpt4 key购买 nike

我正在尝试使用 PDFBox 解析日记页面。这是我正在使用的代码片段:

try (PDDocument document = PDDocument.load(new File("myfile.pdf"))) {

if (!document.isEncrypted()) {

PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);

PDFTextStripper tStripper = new PDFTextStripper();
tStripper.setParagraphEnd("\n");

String pdfFileInText = tStripper.getText(document);

String output = "";

String lines[] = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
output += line + "\n";
}

}

}

问题是,尽管我得到的段落没问题,但它们以完全随机的顺序显示。我需要按自然顺序获取段落(从上到下,从左到右),但 PDFBox 似乎没有任何真正的原因从页面的一侧跳到另一侧。我的原始 PDF 文件还包含随机位置的图像,我认为这可能与此有关。

以下是未按顺序阅读的 PDF 示例:

这是我从该示例中得到的结果:

GALIZA>2-3
Analizamos os programas de PSOE, PP,
En Común-Unidas Podemos e do BNG

> Na Galiza hai case 15 librarías por cada
100.000 habitantes

> Só o 26% das persoas propietarias son
mulleres, fronte ao 74% de homes

A media de
traballadoras dunha
libraría e de 3,5

TRABALLO>15
Día das Librarías

As oito
medidas
electorais
para Galiza

有没有办法让段落按自然顺序排列?

最佳答案

这对你有用吗?

        PDDocument document = PDDocument.load(new File("myfile.pdf"));
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true);
ObjectMapper objectMapper = new ObjectMapper();

for (int p = 0; p <= document.getNumberOfPages(); p++) {
stripper.setStartPage(p);
stripper.setEndPage(p);
String text = stripper.getText(document);
System.out.println(text);
}

可能没有使用使用启发式的PDFTextStripperByArea。只是获取文本然后格式化它?你能试试这个吗?

正如我在评论中所说,不直接查看 pdf 很难进行评估

关于java - 按顺序阅读 PDFBox 中的段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com