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();
    }
}
相关推荐
冬奇Lab10 小时前
开源项目第173期:AI For Beginners — 微软出品的 12 周 24 课 AI 完整课程
人工智能·开源·资讯
lipku12 小时前
数字人直播开源项目LiveStream
python·开源·数字人·数字人直播
疯狂敲代码的老刘12 小时前
百度开源 Unlimited-OCR:一次识别整篇文档,告别逐行扫描时代
开源·ocr
小弥儿13 小时前
GitHub今日热榜 | 2026-08-01:硬件安全工具与AI教育并行
人工智能·学习·开源·github
vipjx214 小时前
百度网盘不限速原理解析:如何合法合规提升你的网盘下载速度
开源
a11177615 小时前
中文优先的企业 RAG 知识库 开源项目
开发语言·开源·kotlin
TunerT_TQ15 小时前
Valhalla 静态工程审阅 #009|Continue 源码证据驱动评测【大厂开源基础设施特辑】
vscode·测试工具·开源·llm·github·jetbrains·ai编程助手
TunerT_TQ16 小时前
Valhalla 静态工程审阅 #008|RisingWave 源码证据驱动评测【大厂开源基础设施特辑】
rust·开源·github·实时数据处理·流式数据库·apacheflink·streamingsql
带娃的IT创业者17 小时前
Inkling:当开源模型开始思考“如何思考”
人工智能·开源·大语言模型·多模态·moe·开源模型·inkling
在水一缸17 小时前
KOReader:当开源精神遇上电子墨水屏,一场阅读体验的静默革命
开源·开源项目·电子墨水屏·极客·kindle·koreader·阅读体验