JAVA实现将PDF转换成word文档

POM.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
<!--       <version>3.2.1</version>-->
       <version>2.3.9.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jack</groupId>
    <artifactId>jackDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jackDemo</name>
    <description>jackDemo</description>
    <properties>
       <java.version>1.8</java.version>
    </properties>
    <dependencies>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-mongodb</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
          <optional>true</optional>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
       </dependency>

       <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.75</version>
       </dependency>


       <dependency>
          <groupId>org.openpnp</groupId>
          <artifactId>opencv</artifactId>
          <version>4.5.3-4</version>
       </dependency>

       <!-- Apache POI for Excel files -->
       <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>5.2.3</version> <!-- 请检查并使用最新版本 -->
       </dependency>

       <!-- Apache POI dependencies (these may be included automatically by Maven, but it's good to be explicit) -->
       <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>5.2.3</version> <!-- 与poi-ooxml版本保持一致 -->
       </dependency>

       <!-- Apache Commons Collections (required by POI) -->
       <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-collections4</artifactId>
          <version>4.4</version> <!-- 确保版本与你的项目兼容 -->
       </dependency>

       <!-- Apache Commons IO (optional, but useful for file handling) -->
       <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.11.0</version> <!-- 确保版本与你的项目兼容 -->
       </dependency>

       <!-- PDFBox for reading PDF files -->
       <dependency>
          <groupId>org.apache.pdfbox</groupId>
          <artifactId>pdfbox</artifactId>
          <version>2.0.24</version>
       </dependency>

       <!-- docx4j for creating Word documents -->
       <dependency>
          <groupId>org.docx4j</groupId>
          <artifactId>docx4j</artifactId>
          <version>3.2.1</version>
       </dependency>

       <dependency>
          <groupId>javax.xml.bind</groupId>
          <artifactId>jaxb-api</artifactId>
          <version>2.3.1</version>
       </dependency>
       <dependency>
          <groupId>org.glassfish.jaxb</groupId>
          <artifactId>jaxb-runtime</artifactId>
          <version>2.3.1</version>
       </dependency>

    </dependencies>


    <build>
       <plugins>
          <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
       </plugins>
    </build>

</project>

java 文件:

复制代码
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.Text;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PdfToWordConverter {

    public static void main(String[] args) throws Exception{
        String pdfFilePath = "D:\\word\\何以为父影响彼此一生的父子关系.pdf"; // 替换为你的PDF文件路径
        String wordFilePath = "D:\\word\\何以为父影响彼此一生的父子关系.docx"; // 生成的Word文件路径

        try {
            // 读取PDF文件内容
            String pdfText = extractTextFromPdf(pdfFilePath);

            // 将内容写入Word文档
            createWordDocument(wordFilePath, pdfText);

            System.out.println("PDF to Word conversion completed successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String extractTextFromPdf(String filePath) throws IOException {
        PDDocument document = PDDocument.load(new FileInputStream(filePath));
        PDFTextStripper pdfStripper = new PDFTextStripper();
        return pdfStripper.getText(document);
    }

    public static void createWordDocument(String filePath, String content) throws Exception {
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
        Body body = mainDocumentPart.getContents().getBody();

        // 将内容按段落分割并添加到Word文档中
        String[] paragraphs = content.split("\\r?\\n");
        for (String paragraph : paragraphs) {
            P p = new P();
            R r = new R();
            Text text = new Text();
            text.setParent(paragraph);
            r.getContent().add(text);
            p.getContent().add(r);
            body.getContent().add(p);
        }

        // 保存Word文档
        try (FileOutputStream out = new FileOutputStream(new File(filePath))) {
            wordMLPackage.save(out);
        }
    }
}
相关推荐
香蕉大香蕉(06敬畏市场)18 分钟前
高频八股 · JVM(8)
java·jvm·面试·八股·大厂实习
指针向南19 分钟前
PDF 转 HTML 免费5种方法,体积0.5%到368%
图像处理·人工智能·计算机视觉·pdf
养军博客26 分钟前
spirngSecurity 鉴权教程02
java·springsecurity
CoderYanger35 分钟前
Java EE 进阶:5. Spring IoC&DI(第二弹):Spring IoC
java·开发语言·spring boot·spring·面试·职场和发展·java-ee
悟天特斯1 小时前
安防一体化:从“各管各的“到“全局联动“
java·大数据·网络·人工智能·物联网
70后写java的老码农2 小时前
Spring Boot 3.x + ShardingSphere 5.x + MyBatis-Plus 3.x:订单分库分表实战(含两个踩坑记录)
java
霸道流氓气质2 小时前
AI 翻译插件:翻译记忆级联复用、术语混合检索与结构化质量门禁的 Vue3 + Spring AI 生产级实战
java·人工智能·spring
斯维赤2 小时前
OCR到底是接入第三方API 还是自己封装?
java·ocr
码智社2 小时前
Redis 哨兵模式详解:从原理到生产实践
java·redis
救救孩子把2 小时前
结构化输出为何“格式正确、答案错误”?JSON Schema 的能力边界
android·java·json·agent·agent服务