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();
    }
}
相关推荐
da-peng-song23 分钟前
python学习—详解word邮件合并
python·学习·word
陈哥聊测试2 小时前
这款自研底层框架,你说不定已经用上了
前端·后端·开源
Luckyfif2 小时前
🗣️面试官:有一个比较经典的 case 也想探讨一下 「页面白屏如何排查?」❤️✨
前端·面试·开源
蚝油菜花3 小时前
这个开源AI平台把文生图/音/字全包了!Pollinations.AI:提供完全免费的AI内容生成
人工智能·开源
爱的叹息3 小时前
公司内部自建知识共享的方式分类、详细步骤及表格总结,分为开源(对外公开)和闭源(仅限内部),以及公共(全员可访问)和内部(特定团队/项目组)四个维度
开源
蚝油菜花3 小时前
测试工程师要失业?Magnitude:开源AI Agent驱动的端到端测试框架,让Web测试更智能,自动完善测试用例!
人工智能·开源
努力犯错玩AI4 小时前
16.4B参数仅激活2.8B!Kimi-VL-A3B开源:长文本、多模态、低成本的AI全能选手
人工智能·后端·开源
SnXJi_5 小时前
纷析云开源财务软件:助力企业财务管理数字化转型
java·gitee·开源·开源软件·gitcode
SnXJi_11 小时前
纷析云开源财务软件:企业财务数字化转型的灵活解决方案
java·gitee·开源·开源软件