使用PDFBOX提取PDF中的文字

            PDDocument pdf = PDDocument.load(new File(srcFilePath));
            PDPageTree pageTree = pdf.getPages();
            int count = pageTree.getCount();
            returnList = new String[count];
            PDFTextStripper stripper = new PDFTextStripper() {
                @Override
                protected void startPage(PDPage page) throws IOException {
                    startOfLine = true;
                    super.startPage(page);
                }
                @Override
                protected void writeLineSeparator() throws IOException {
                    startOfLine = true;
                    super.writeLineSeparator();
                }
                @Override
                protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
                    if (startOfLine) {
                        TextPosition firstProsition = textPositions.get(0);
                        writeString(String.format("[%s,%s]", firstProsition.getXDirAdj(), firstProsition.getYDirAdj()));
                        startOfLine = false;
                    }
                    super.writeString(text, textPositions);
                }
                boolean startOfLine = true;
            };
            stripper.setSortByPosition(false);
            stripper.setStartPage(1);
            stripper.setEndPage(count);
            String content = stripper.getText(pdf);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PDFBox 是一个用于处理 PDF 文档的 Java 库。要按块提取 PDF 文档文字内容,可以使用 PDFTextStripper 类提供的方法。以下是一个示例代码: ```java import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.TextPosition; public class TextStripperWithPosition extends PDFTextStripper { public TextStripperWithPosition() throws IOException { super(); } @Override protected void writeString(String string, List<TextPosition> textPositions) throws IOException { // 处理每个文本块 for (TextPosition text : textPositions) { System.out.println("坐标:" + text.getXDirAdj() + ", " + text.getYDirAdj()); System.out.println("内容:" + text.getUnicode()); } } public static void main(String[] args) throws IOException { String fileName = "test.pdf"; try (PDDocument document = PDDocument.load(new File(fileName))) { PDFTextStripper stripper = new TextStripperWithPosition(); stripper.setSortByPosition(true); stripper.setStartPage(0); stripper.setEndPage(document.getNumberOfPages()); // 提取文字内容 stripper.getText(document); } } } ``` 在这个示例,我们继承了 PDFTextStripper 类,并重写了 writeString 方法。这个方法会在 PDF 文档找到一个文本块时被调用。我们可以从 TextPosition 对象获取每个文本块的坐标和内容。在 main 方法,我们创建一个 TextStripperWithPosition 对象,并设置了一些参数,然后调用 getText 方法来提取 PDF 文档文字内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值