如何用java代码实现word转pdf

pom

xml 复制代码
  <!-- word -> pdf -->
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.0.3</version>
        </dependency>

代码

java 复制代码
   @Test
    public void pdf232() {
        long l = System.currentTimeMillis();
        String wordPath = "E:\\歌\\2022_江区_1716896947404\\日常\\2_取水户建月现场验.docx";
        String pdfPath = "E:\\歌\\2022_江区_1716896947404\\日常\\2_水户中每月现验.pdf";
        try {
            System.out.println(wordPath + "  开始转换.....");
            InputStream docxInputStream = new FileInputStream(wordPath);
            OutputStream outputStream = new FileOutputStream(pdfPath);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            long l2 = System.currentTimeMillis();
            System.out.println(l2-l);
            System.out.println(pdfPath + "  转换成功.....");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("导出成功......");
    }

效果

参考文章

https://blog.csdn.net/m0_59828167/article/details/139070897

相关推荐
今天你TLE了吗1 小时前
LLM到Agent&RAG——AI知识点概述 第六章:Function Call函数调用
java·人工智能·学习·语言模型·大模型
Rcnhtin1 小时前
RocketMQ
java·linux·rocketmq
JH30731 小时前
RedLock-红锁
java·redis
rannn_1112 小时前
【Redis|原理篇2】Redis网络模型、通信协议、内存回收
java·网络·redis·后端·缓存
遇见你的雩风3 小时前
网络原理(一)
java·网络
952363 小时前
Spring IoC&DI
java·数据库·spring
十六年开源服务商3 小时前
游戏与设计驱动WordPress建站2026
java·前端·游戏
前进吧-程序员3 小时前
C++ 内存到底分配在哪?
java·jvm·c++
NWU_白杨3 小时前
VoiceMockInterview项目MVP开发
java·ai
RDCJM3 小时前
Springboot的jak安装与配置教程
java·spring boot·后端