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();
    }
}
相关推荐
Van_Moonlight1 天前
RN for OpenHarmony 实战 TodoList 项目:已完成未完成数量显示
javascript·开源·harmonyos
济南壹软网络科技有限公司1 天前
基于 ThinkPHP 8.1 + Workerman 的全开源商业级游戏陪玩系统技术架构设计
游戏·开源·游戏陪玩·php护航·商业版游戏护航
大厂技术总监下海1 天前
大数据生态的“主动脉”:RocketMQ 如何无缝桥接 Flink、Spark 与业务系统?
大数据·开源·rocketmq
bu_shuo1 天前
将AI生成的数学公式正确复制到word中
人工智能·chatgpt·word·latex
玖日大大1 天前
Milvus 深度解析:开源向量数据库的技术架构、实践指南与生态生态
数据库·开源·milvus
这儿有一堆花1 天前
MusicFree:开源多平台聚合音乐软件
开源·github
Kagol1 天前
🎉TinyPro v1.4.0 正式发布:支持 Spring Boot、移动端适配、新增卡片列表和高级表单页面
前端·typescript·开源
为自己_带盐1 天前
在 Blazor Server 中集成 docx-preview.js 实现高保真 Word 预览
javascript·c#·word
不知疲倦的仄仄1 天前
第五天:深度解密 Netty ByteBuf:高性能 IO 的基石
java·开源·github
公子如琢1 天前
在word方框中打√符号☑☒
word