是否可以使用 Apache Tika 按页提取 word/pdf 文件中的文本?

发布于 2024-11-04 06:54:27 字数 73 浏览 6 评论 0原文

我能找到的所有文档似乎都表明我只能提取整个文件的内容。但我需要单独提取页面。我需要为此编写自己的解析器吗?我是否缺少一些明显的方法?

All the documentation I can find seems to suggest I can only extract the entire file's content. But I need to extract pages individually. Do I need to write my own parser for that? Is there some obvious method that I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心荒芜 2024-11-11 06:54:27

实际上,Tika 确实通过在页面开始之前发送元素

来处理页面(至少在 pdf 中)页面结束后。您可以使用它轻松地在处理程序中设置页数(仅使用

来计算页数):

public abstract class MyContentHandler implements ContentHandler {
    private String pageTag = "p";
    protected int pageNumber = 0;
    ...
    @Override
    public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException  {  

        if (pageTag.equals(qName)) {
            startPage();
        }
    }

    @Override
    public void endElement (String uri, String localName, String qName) throws SAXException {  

        if (pageTag.equals(qName)) {
            endPage();
        }
    }

    protected void startPage() throws SAXException {
    pageNumber++;
    }

    protected void endPage() throws SAXException {
    return;
    }
    ...
}

当使用 pdf 执行此操作时,当解析器不发送文本行时,您可能会遇到问题正确的顺序 - 请参阅 使用 Apache 从 PDF 文件中提取文本Tika 0.9(以及底层的 PDFBox) 介绍了如何处理这个问题。

Actually Tika does handle pages (at least in pdf) by sending elements <div><p> before page starts and </p></div> after page ends. You can easily setup page count in your handler using this (just counting pages using only <p>):

public abstract class MyContentHandler implements ContentHandler {
    private String pageTag = "p";
    protected int pageNumber = 0;
    ...
    @Override
    public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException  {  

        if (pageTag.equals(qName)) {
            startPage();
        }
    }

    @Override
    public void endElement (String uri, String localName, String qName) throws SAXException {  

        if (pageTag.equals(qName)) {
            endPage();
        }
    }

    protected void startPage() throws SAXException {
    pageNumber++;
    }

    protected void endPage() throws SAXException {
    return;
    }
    ...
}

When doing this with pdf you may run into the problem when parser doesn't send text lines in proper order - see Extracting text from PDF files with Apache Tika 0.9 (and PDFBox under the hood) on how to handle this.

谈场末日恋爱 2024-11-11 06:54:27

您需要使用底层库 - Tika 在页面级别不执行任何操作。

对于 PDF 文件,PDFBox 应该能够为您提供一些页面内容。对于 Word,Apache POI 中的 HWPF 和 XWPF 并不真正执行页面级别的操作 - 分页符不存储在文件中,而是需要根据文本 + 字体 + 页面大小动态计算...

You'll need to work with the underlying libraries - Tika doesn't do anything at the page level.

For PDF files, PDFBox should be able to give you some page stuff. For Word, HWPF and XWPF from Apache POI don't really do page level things - the page breaks aren't stored in the file, but instead need to be calculated on the fly based on the text + fonts + page size...

桃扇骨 2024-11-11 06:54:27

您可以使用 元数据获取 Pdf 中的页数 对象的 xmpTPg:NPages 键如下所示:

Parser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext();
parser.parse(fis, handler, metadata, parseContext);
metadata.get("xmpTPg:NPages");

You can get the number of pages in a Pdf using the metadata object's xmpTPg:NPages key as in the following:

Parser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext();
parser.parse(fis, handler, metadata, parseContext);
metadata.get("xmpTPg:NPages");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文