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

相关推荐
CoderYanger3 小时前
Java EE 进阶:16. Spring原理(第一弹)
java·服务器·数据库·后端·spring·java-ee·github
开开心心就好3 小时前
游戏安装报错缺环境?运行库整合包补上
java·mysql·游戏·pdf·ocr·音视频·lua
SunnyDays10113 小时前
Java 裁切 PDF 页面:去除多余白边、批量裁剪与局部图片导出
java·开发语言·pdf
m0_734172423 小时前
用 Node.js 管理临时工作目录
java
xixingzhe23 小时前
微服务四大保护机制:限流、重试、熔断、降级
java·微服务·架构
好好沉淀3 小时前
Spring Boot 中 consumes 和 isSaveRequestData=false 到底有什么用?
java·后端·ai编程
lgbisha4 小时前
银河麒麟入门:日志刷满终端,用 less 定位关键词并看前后几行
java·前端·less·银河麒麟
xpbobcsdn4 小时前
JDK 28 值类:省一半内存,慢三成
java·开发语言
Monkey的自我迭代4 小时前
Kubernetes 声明式理念:只写“我要什么”
java·学习·kubernetes
Shaoshing4 小时前
LeetCode 1798. 你能构造出连续值的最大数目
java·数据结构·算法