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

相关推荐
Wang153027 分钟前
Java编程基础与面向对象核心概念
java
毕设源码-郭学长27 分钟前
【开题答辩全过程】以 康复管理系统为例,包含答辩的问题和答案
java
毅炼31 分钟前
hot100打卡——day17
java·数据结构·算法·leetcode·深度优先
winfreedoms34 分钟前
java-网络编程——黑马程序员学习笔记
java·网络·学习
开开心心就好1 小时前
键盘改键工具免安装,自定义键位屏蔽误触
java·网络·windows·随机森林·计算机外设·电脑·excel
IManiy1 小时前
总结之Temporal全局速率控制(二)第三方速率控制服务设计
java
OpenMiniServer1 小时前
电气化能源革命下的社会
java·人工智能·能源
独自破碎E1 小时前
LCR_019_验证回文串II
java·开发语言
坚持就完事了1 小时前
Java中的一些关键字
java·开发语言
寄存器漫游者1 小时前
数据结构 C语言 顺序栈
java·c语言·数据结构