餐饮网站系统,栾城区住房建设局官方网站,电商平台搭建八个步骤,杭州工业设计公司最近搞到一个任务是要解析一套雅思题目并提取其中的高频单词。那如何使用java来解析一个pdf文件呢#xff1f;
首先我们要知道这需要springboot框架来进行创建#xff0c;需要的PDFTextStripper是一个用于PDF文档中提取文本的类#xff0c;它是Apache PDFBox的一个类用于处…最近搞到一个任务是要解析一套雅思题目并提取其中的高频单词。那如何使用java来解析一个pdf文件呢
首先我们要知道这需要springboot框架来进行创建需要的PDFTextStripper是一个用于PDF文档中提取文本的类它是Apache PDFBox的一个类用于处理PDF文档的开源的库。其主要功能是解析文档的内容流所以我们需要定义一个流来提取其中的文字内容。
所以我们先引入pdfbox相关的依赖具体如下
dependencygroupIdorg.apache.pdfbox/groupIdartifactIdfontbox/artifactIdversion2.0.0/version
/dependency
dependencygroupIdorg.apache.pdfbox/groupIdartifactIdjempbox/artifactIdversion1.8.11/version
/dependency
dependencygroupIdorg.apache.pdfbox/groupIdartifactIdxmpbox/artifactIdversion2.0.0/version
/dependency
dependencygroupIdorg.apache.pdfbox/groupIdartifactIdpreflight/artifactIdversion2.0.0/version
/dependency
dependencygroupIdorg.apache.pdfbox/groupIdartifactIdpdfbox-tools/artifactIdversion2.0.0/version
/dependency这是比较完整的一套依赖。我们把提交pdf和接收pdf用一种post的方式进行上传这样会显得提交方式非常的灵活。
!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleUpload PDF/title
/head
body
h1Upload PDF File/h1
form action/api/files/upload-pdf methodpost enctypemultipart/form-datainput typefile namefile acceptapplication/pdf requiredbutton typesubmitUpload/button
/form
/body
/html然后我们简单写一个controller
RestController
RequestMapping(/api/files)
public class ReadFileController {Autowiredprivate ExtractService extractService;PostMapping(/upload-pdf)public ResponseEntityObject uploadPdf(RequestParam(file) MultipartFile file) {// 验证文件是否为空if (file.isEmpty()) {return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(File is empty);}// 验证文件类型是否为PDFif (!application/pdf.equals(file.getContentType())) {return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Only PDF files are allowed);}String file_name file.getOriginalFilename();String filePath extractService.Run(file,file_name);我们可以看到表单提交的action就是我们controller里面的路径提交之后我们在做一个简单的文件类型判断之后就返回给了业务层进行解析。 private StringBuilder accumulatedText new StringBuilder();
public ListMap.EntryString, Integer read_file(MultipartFile file) {try (InputStream inputStream file.getInputStream()) {try (PDDocument doc PDDocument.load(inputStream)) {PDFTextStripper textStripper new PDFTextStripper();textStripper.setSortByPosition(true);String allText textStripper.getText(doc);accumulatedText.append(allText).append(\n);}} catch (IOException e) {e.printStackTrace();}return getSortedWordList(accumulatedText.toString());}这段代码首先通过file.getInputStream()获取上传文件对应的输入流这个过程就避免了我们先把文件存到磁盘而是直接从文件中读数据。PDDocument是Apache PDFBox库中的一个类用于表示PDF文档。此语句确保了PDDocument对象在使用后会被正确关闭。PDFTextStripper 是 Apache PDFBox 库中的一个类用于从 PDF 文档中提取文本。它提供了一种简单而有效的方法来遍历 PDF 的内容并将其中的文本元素抽取出来形成字符串。PDFTextStripper 可以解析 PDF 页面上的文本对象并将它们转换为可读的字符串格式。通过设置 setSortByPosition(true)可以确保提取的文本按照其在页面上的实际位置进行排序这有助于保持文本的自然阅读顺序。默认情况下PDFTextStripper 按照文本对象在 PDF 文件中的出现顺序提取文本这可能会导致文本顺序混乱。
StringBuilder 是 Java 中的一个类它提供了可变的字符序列允许你高效地构建、修改和操作字符串。与 String 类不同String 是不可变的immutable每次对 String 的修改都会创建一个新的 String 对象而 StringBuilder 是可变的mutable可以在同一个对象上进行多次修改而不创建新的对象。这使得 StringBuilder 在需要频繁修改字符串的情况下更加高效。 尽管 PDFTextStripper 主要用于文本提取但它也可以与 PDFStreamEngine 结合使用以处理 PDF 中的其他内容如图像或矢量图形。目前我还没有用到日后需要解析非文字内容再做解析。