1. 基本知识
apache pdfbox 是一个开源的 java pdf 操作库,支持:
读取 pdf 文件内容(包括文字、图片、元数据)
创建和修改 pdf 文档
提取文本内容用于搜索、分析等操作
maven相关的依赖:
<dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox</artifactid> <version>2.0.29</version> </dependency>
需下载 在进行统计:
import org.apache.pdfbox.pdmodel.pddocument; import org.apache.pdfbox.text.pdftextstripper; import java.io.file; import java.io.ioexception; public class pdfwordcounter { public static void main(string[] args) { string pdfpath = "sample.pdf"; // 替换为你的 pdf 文件路径 string keyword = "java"; // 要统计的词语 try { // 加载 pdf 文档 pddocument document = pddocument.load(new file(pdfpath)); // 使用 pdftextstripper 提取文本 pdftextstripper stripper = new pdftextstripper(); string text = stripper.gettext(document); document.close(); // 记得关闭文档资源 // 转小写处理,方便忽略大小写 string lowertext = text.tolowercase(); string lowerkeyword = keyword.tolowercase(); // 调用词频统计函数 int count = countoccurrences(lowertext, lowerkeyword); system.out.println("词语 \"" + keyword + "\" 出现次数: " + count); } catch (ioexception e) { e.printstacktrace(); } } // 使用 indexof 遍历匹配词语出现次数 private static int countoccurrences(string text, string word) { int count = 0; int index = 0; while ((index = text.indexof(word, index)) != -1) { count++; index += word.length(); } return count; } }
上述的demo详细分析下核心知识:
pddocument.load(file)
用于加载 pdf 文件到内存中
pdfbox 使用 pddocument 表示整个 pdf 对象,使用完后必须调用 close() 释放资源pdftextstripper
pdfbox 中用于提取文字的核心类,会尽可能“以阅读顺序”提取文本,适用于纯文字 pdf 文件。对于图像型扫描件则无效(需 ocr)大小写不敏感统计
实际应用中搜索关键词通常需要忽略大小写,因此我们先统一将文本和关键词转换为小写indexof 实现词频统计
这是最基础也最直观的统计方法,效率较高,但不够精确
如果需要更精确(只统计完整单词),可以使用正则:
pattern pattern = pattern.compile("\\b" + pattern.quote(word) + "\\b", pattern.case_insensitive); matcher matcher = pattern.matcher(text); int count = 0; while (matcher.find()) { count++; }
2. 在线url
2.1 英文
此处的demo需要注意一个点:
注意点 | 说明 |
---|---|
pdf 文件是否公开访问 | 不能访问受密码或登录保护的 pdf |
文件大小 | 不建议下载和分析过大文件,可能导致内存问题 |
中文 pdf | 若是扫描图片形式的中文 pdf,则 pdfbox 无法直接提取文本(需 ocr) |
编码问题 | 若中文显示为乱码,可能是 pdf 没有内嵌字体 |
思路:
通过 url.openstream() 获取在线 pdf 的输入流
使用 pdfbox 的 pddocument.load(inputstream) 读取 pdf
用 pdftextstripper 提取文本
用字符串方法或正则统计关键词频率
import org.apache.pdfbox.pdmodel.pddocument; import org.apache.pdfbox.text.pdftextstripper; import java.io.inputstream; import java.net.url; import java.util.regex.matcher; import java.util.regex.pattern; public class onlinepdfkeywordcounter { public static void main(string[] args) { string pdfurl = "https://www.example.com/sample.pdf"; // 你的在线 pdf 链接 string keyword = "java"; // 需要统计的关键词 try (inputstream inputstream = new url(pdfurl).openstream(); pddocument document = pddocument.load(inputstream)) { pdftextstripper stripper = new pdftextstripper(); string text = stripper.gettext(document); // 使用正则匹配单词边界(忽略大小写) pattern pattern = pattern.compile("\\b" + pattern.quote(keyword) + "\\b", pattern.case_insensitive); matcher matcher = pattern.matcher(text); int count = 0; while (matcher.find()) { count++; } system.out.println("词语 \"" + keyword + "\" 出现在在线 pdf 中的次数为: " + count); } catch (exception e) { system.err.println("处理 pdf 时出错: " + e.getmessage()); e.printstacktrace(); } } }
2.2 混合
方法 | 适用场景 | 是否支持中文 |
---|---|---|
indexof | 中英文都适用 | ✅ |
pattern + \\b | 仅限英文单词匹配 | ❌ 中文不支持 |
正则表达式 \\b...\\b
(表示“单词边界”)并不适用于中文
统计在想的url pdf的词频:
import org.apache.pdfbox.pdmodel.pddocument; import org.apache.pdfbox.text.pdftextstripper; import java.io.inputstream; import java.net.url; public class onlinepdfkeywordcounter { public static void main(string[] args) { string pdfurl = "https://www.xxxx.pdf"; string keyword = "管理层"; // 要统计的中文关键词 try (inputstream inputstream = new url(pdfurl).openstream(); pddocument document = pddocument.load(inputstream)) { pdftextstripper stripper = new pdftextstripper(); string text = stripper.gettext(document); // 直接用 indexof 不区分大小写(对于中文没必要转小写) int count = countoccurrences(text, keyword); system.out.println("词语 \"" + keyword + "\" 出现次数为: " + count); } catch (exception e) { system.err.println("处理 pdf 时出错: " + e.getmessage()); e.printstacktrace(); } } // 简单统计子串出现次数(适用于中文) private static int countoccurrences(string text, string keyword) { int count = 0; int index = 0; while ((index = text.indexof(keyword, index)) != -1) { count++; index += keyword.length(); } return count; } }
截图如下:
以上就是java使用pdfbox提取pdf文本并统计关键词出现的次数的详细内容,更多关于java pdfbox提取pdf文本并统计的资料请关注代码网其它相关文章!
发表评论