Spring Boot写一个简单的PDF到Word的转换程序

使用Spring Boot创建PDF到Word的转换程序需要几个步骤。可以使用现有的库来处理转换过程。使用ApachePDFBox进行PDF操作和使用ApachePOI创建Word文档的过程。

  1. 添加依赖项:

    将以下依赖项添加到"pom.xml"文件中,以在项目中包括PDFBox和POI:

    xml 复制代码
    <dependencies>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.27</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>
  2. 创建 Service:

    创建一个处理PDF到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 org.apache.poi.xwpf.usermodel.XWPFRun;
    import org.springframework.stereotype.Service;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    @Service
    public class PdfToWordConverterService {
    
        public byte[] convertPdfToWord(byte[] pdfBytes) throws IOException {
            try (PDDocument pdfDocument = PDDocument.load(new ByteArrayInputStream(pdfBytes));
                 XWPFDocument wordDocument = new XWPFDocument()) {
    
                PDFTextStripper pdfTextStripper = new PDFTextStripper();
                String text = pdfTextStripper.getText(pdfDocument);
    
                XWPFParagraph paragraph = wordDocument.createParagraph();
                XWPFRun run = paragraph.createRun();
                run.setText(text);
    
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                wordDocument.write(outputStream);
                return outputStream.toByteArray();
            }
        }
    }
  3. 创建控制器:

    创建一个Spring MVC控制器来处理传入的请求。此控制器应使用"PdfToWordConverterService"来执行转换.

    java 复制代码
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class PdfToWordController {
    
        @Autowired
        private PdfToWordConverterService converterService;
    
        @PostMapping("/convert")
        public byte[] convertPdfToWord(@RequestBody byte[] pdfBytes) throws IOException {
            return converterService.convertPdfToWord(pdfBytes);
        }
    }
相关推荐
AI玫瑰助手1 分钟前
PDF 转 Markdown 主力方案怎么选:PaddleOCR-VL-1.5、MinerU、HunyuanOCR 与 MonkeyOCR 实测对比
pdf·大模型·知识库·表格·md·文字识别·paddleocr
海兰16 分钟前
【第54篇】Graph + Langfuse 可观测性实战
java·人工智能·spring boot·spring ai
qq_5470261791 小时前
SpringBoot + Redis 电商秒杀完整方案
spring boot·redis·后端
其实秋天的枫1 小时前
【2026年5月】初级会计师考试真题试卷及答案PDF
经验分享·pdf
那个失眠的夜1 小时前
SpringBoot
java·开发语言·spring boot·spring·mvc·mybatis
多敲代码防脱发1 小时前
Spring进阶(Bean的生命周期与Bean的后处理器)
java·服务器·开发语言·spring boot·spring·servlet
voyaqi1 小时前
从零设计企业级校验框架:Spring Boot + SPI 实战指南
spring boot·后端·log4j
小坏讲微服务2 小时前
SpringBoot整合SpringAI配置多平台API密钥
java·人工智能·spring boot·后端·flask·ai编程·claude code
小为资料库2 小时前
英语六级听力历年真题汇总电子版PDF(含听力音频mp3、答案解析及听力原文)
pdf·音视频
Metaphor6923 小时前
使用 Python 设置 Word 文档文本的颜色
python·word