是否可以使用 Apache Tika 按页提取 word/pdf 文件中的文本?
我能找到的所有文档似乎都表明我只能提取整个文件的内容。但我需要单独提取页面。我需要为此编写自己的解析器吗?我是否缺少一些明显的方法?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,Tika 确实通过在页面开始之前发送元素
和
来处理页面(至少在 pdf 中)页面结束后。您可以使用它轻松地在处理程序中设置页数(仅使用
来计算页数):
当使用 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>
):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.
您需要使用底层库 - 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...
您可以使用 元数据获取 Pdf 中的页数 对象的
xmpTPg:NPages
键如下所示:You can get the number of pages in a Pdf using the metadata object's
xmpTPg:NPages
key as in the following: