Java使用PDFBox提取PDF文本并统计关键词出现的次数
作者:码农研究僧
这篇文章主要介绍了Apache PDFBox库的基本知识,包括如何使用PDDocument加载PDF文件、PDFTextStripper提取文本以及如何进行词频统计,还提供了在线URL的处理方法,需要的朋友可以参考下
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文本并统计的资料请关注脚本之家其它相关文章!