当前位置: 首页 > news >正文

PDF 转Word 开源库

1. Apache PDFBox

Apache PDFBox 是一个开源的 Java 库,用于创建和操作 PDF 文档。虽然 PDFBox 本身没有直接支持 PDF 转 Word 的功能,但它可以提取 PDF 内容,你可以结合其他方法将这些内容写入 Word。

添加依赖

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.24</version> <!-- 检查是否有最新版本 -->
</dependency>

使用示例

你可以使用 PDFBox 提取文本,然后使用 Apache POI 库将其写入 Word 文档。

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class PdfToWordConverter {public static void main(String[] args) {String pdfFilePath = "path/to/input.pdf";String wordFilePath = "path/to/output.docx";try {convertPdfToWord(pdfFilePath, wordFilePath);System.out.println("PDF converted to Word successfully!");} catch (IOException e) {e.printStackTrace();}}public static void convertPdfToWord(String pdfFilePath, String wordFilePath) throws IOException {PDDocument document = PDDocument.load(new FileInputStream(pdfFilePath));PDFTextStripper stripper = new PDFTextStripper();String pdfText = stripper.getText(document);document.close();// 创建 Word 文档并写入文本XWPFDocument wordDocument = new XWPFDocument();XWPFParagraph paragraph = wordDocument.createParagraph();paragraph.createRun().setText(pdfText);try (FileOutputStream out = new FileOutputStream(wordFilePath)) {wordDocument.write(out);}wordDocument.close();}
}

http://www.mrgr.cn/news/4789.html

相关文章:

  • 每日一题-贪心算法
  • 【业余玩儿AI】【文档问答】实操记录0822
  • Redis7基础篇(一)
  • 【YashanDB认证】YCA学习记录
  • xss 漏洞复现
  • git 指令
  • 计算机四个方面:计算、存储、通信与程序;操作系统
  • ps磨皮滤镜插件Imagenomic Portraiture 4.5 Build 4501中文版
  • 汇编语言:标志寄存器ZF、PF、SF、CF、OF、DF、IF、AF
  • EmguCV学习笔记 VB.Net 2.5 Mat类、Matrix类和Image类的相互转换
  • keepalive原理详解及应用
  • insmod后发生了什么
  • Linux 软件编程学习第十七天
  • 【图像超分】论文精读:AdaBM: On-the-Fly Adaptive Bit Mapping for Image Super-Resolution
  • Web前端:CSS篇(三)盒子模型,弹性盒子
  • 无人机之喊话器的用途
  • 基于深度学习的位置感知应用
  • esbuild中的Text Loader:简化文本文件处理
  • 【C++初阶】:C++入门篇(一)
  • chmod命令学习1