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();
    }
}
相关推荐
刘发财2 小时前
前端如何生成10000页PDF文件?
前端·javascript·开源
Erishen2 小时前
Lume C11编译器内部架构:从树形解释器到双LLVM后端的设计之旅
架构·开源·编译器
冬奇Lab2 小时前
一天一个开源项目(第234篇):CC Switch —— 一个 App 管理十款 AI 编程工具的 Provider、MCP、Skills 和 Prompts
人工智能·开源·claude
小蠢驴打代码2 小时前
给 Codex Plan 模式加一本决策账本:问过什么、选了什么,都进仓库
开源·openai·ai编程
程序员老赵7 小时前
Docker 部署 Portainer CE:轻松搭建容器管理平台
docker·容器·开源
TTATTC8 小时前
Gmail、Drive、日历共用一套命令:gws 如何在运行时把 Discovery 文档编译成 CLI
docker·开源
feasibility.8 小时前
“美版DeepSeek”等海外开放模型,能夺回生态入口吗?——中国开放权重领先下的2026全球AI分水岭
人工智能·开源·大模型·llm·minimax·deepseek·beam
缘友一世9 小时前
开源 Linux截图工具闪构(SnapForge)
linux·运维·ubuntu·开源·截图
tianyatest10 小时前
给 AI Agent 写了个 Skill:扫描版电子书自动解压合并 + 加书签(附 PDG 格式逆向)
ai·pdf·电子书
鶴哥只手遮天11 小时前
第 06 章 SQLite 持久化契约与数据模型
人工智能·开源