【问题标题】:Maintaining Structure of Pdf while Extracting Data在提取数据时维护 Pdf 的结构
【发布时间】:2016-06-03 07:34:23
【问题描述】:

我正在尝试使用 iText 库从 PDF 文档中提取数据。我能够提取 PDF 文档,但我无法在解析时维护 PDF 文件的结构。

我也尝试过使用 Apache Tika 和 pdfbox,但我仍然无法维护文件的结构。到目前为止我尝试过的代码如下:

import java.io.PrintWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

public class ExtractPageContent {

    public static final String source = "C:/Users/xyz/Test.pdf";

    public static final String dest = "C:/Users/xyz/Test.txt";

    public void parsePdf(String pdf, String txt) throws IOException {
        PdfReader reader = new PdfReader(pdf);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        TextExtractionStrategy strategy;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            out.println(strategy.getResultantText());
        }
        out.flush();
        out.close();
        reader.close();
    }
    public static void main(String[] args) throws IOException {
        new ExtractPageContent().parsePdf(source, dest);
    }
}

【问题讨论】:

  • 维护PDF的结构是什么意思?
  • 请参阅附加在 url i.stack.imgur.com/pn1GQ.png 中的图像当我使用 pdfbox 或 itext 时,它包含 2 列,以水平直线读取。必须先看左栏,再看右栏
  • 永远不要将更多信息放入 cmets。改为编辑您的问题。下一次:查看帮助中心,了解“好问题”应提供哪些信息。
  • 当我使用 pdfbox 或 itext 以水平直线读取时,它包含 2 列 - iText 和 PDFBox 中的文本提取器都希望文本以阅读顺序绘制,不需要排序(在 iText 中:SimpleTextExtractionStrategy,在 PDFBox PDFTextStripper.setSortByPosition(false) 中),或者将其排序为单列和水平(在 iText 中:LocationTextExtractionStrategy,在 PDFBox PDFTextStripper.setSortByPosition(true) 中)。您可以尝试改进任一库中的代码,以首先尝试识别列并进行相应的提取。 认识并非易事。

标签: java pdf itext


【解决方案1】:

试试这个..它对我有用。您需要使用 PDFBox api。它是从 pdf 文档中提取文本的最佳库。

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.PDFTextStripperByArea;

public void readText(){
try {
    PDDocument document = null;
    document = PDDocument.load(new File("test.pdf"));
    document.getClass();
    if (!document.isEncrypted()) {
        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
        stripper.setSortByPosition(true);
        PDFTextStripper Tstripper = new PDFTextStripper();
        String st = Tstripper.getText(document);
        System.out.println("Text:" + st);
    }
} catch (Exception e) {
    e.printStackTrace();
}
}

【讨论】:

  • 您的代码没有使用PDFTextStripperByArea stripper 做任何事情,是吗?它以何种方式维护文件的结构作为 OP 的要求?
  • 请参阅附在 url i.stack.imgur.com/pn1GQ.png 中的图片当我使用 pdfbox 或 itext 以水平直线读取时,它包含 2 列。必须先看左栏,再看右栏
  • @mkl .. 我已经使用 stripper 对象将按位置属性设置为 true...stripper.setSortByPosition(true);
  • 我使用了剥离器对象来设置按位置属性为 true 的排序 - 但由于您不使用 stripper 而是使用 Tstripper 进行提取,所以 sort通过stripper 的位置属性 完全没有区别。因此,您不会将stripper 用于任何事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 2010-11-05
  • 2014-10-09
  • 1970-01-01
相关资源
最近更新 更多