如何用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

相关推荐
杨侨治1 分钟前
Web后端开发概述&环境搭建&项目创建&servlet生命周期
java·笔记·学习·servlet·java基础·javaee
Geeker5512 分钟前
如何从iPhone恢复错误删除的照片
android·数据库·macos·ios·pdf·手机·iphone
游王子40 分钟前
Springboot与xxl-job
java·spring boot·后端
秋刀prince1 小时前
【JD-GUI】MacOS 中使用Java反编译工具JD-GUI
java·macos·策略模式
CHNMSCS1 小时前
Django项目 - 合并PDF文件
python·django·pdf
青云交1 小时前
Java面试题--JVM大厂篇之G1 GC的分区管理方式如何减少应用线程的影响
java·垃圾回收器(gc)的性能·区域划分(region)·并行和并发回收·区域优先回收·可预测的停顿时间·jvm算法
codefly-xtl2 小时前
探索 Java 8 中的 Stream API
java
Jack_hrx2 小时前
在 Java 中从 for 循环中移出元素的原理及解决方案
java·循环·for循环·数组下标越界
分享者花花2 小时前
如何从华为恢复永久删除的视频?
android·华为·智能手机·word·手机·cocoa·iphone
科学的发展-只不过是读大自然写的代码2 小时前
qt 读写json格式的文件应用举例 工具函数
java·qt·json