PDF 转Word 开源库

1. Apache PDFBox

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

添加依赖

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

使用示例

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

java 复制代码
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();
    }
}
相关推荐
MikeInNJ44 分钟前
UART 通信协议开源库调研总结
开源·电子
m4Rk_1 小时前
【论文阅读】Agent 记忆机制(48):Fine-Mem——用细粒度奖励解决长期记忆管理中的奖励稀疏与信用分配
论文阅读·人工智能·学习·开源·github
vipjx11 小时前
2026百度网盘不限速下载指南:PanDownload最新复活版与满速提速方案
开源
小弥儿1 小时前
GitHub今日热榜 | 2026-08-23:终端编码Agent与Skills生态扎堆
学习·开源·github
Source.Liu2 小时前
【A11】基于 PDF 渲染引擎的统一文档界面架构设计
rust·pdf
章老师说2 小时前
壬远 AI 网关 v0.4.0 正式发布:模型定价、RMB 配额与多 Key 路由,让企业级 AI 流量治理再进一步
人工智能·ai·开源·负载均衡·ai-native
冰风漫天2 小时前
我也整个开源的MES系统把玩
开源
dong_junshuai3 小时前
# 每天一个开源项目#76 Skills:23万星的 Agent 工程技能库
开源·github·agent
Databuff3 小时前
两款开源APM工具【skywalking+databuff】组合落地实战
开源·skywalking
程序员差不多先生3 小时前
OpenAI 把 Codex Harness 开源:AI Agent 的“操作系统“之争正式开打
人工智能·开源