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

相关推荐
0xDevNull6 小时前
Java反射机制深度解析:从原理到实战
java·开发语言·后端
华科易迅6 小时前
MybatisPlus增删改查操作
android·java·数据库
standovon7 小时前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
IAUTOMOBILE7 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
hutengyi7 小时前
PostgreSQL版本选择
java
皮皮林5517 小时前
重磅!JetBrains 正式发布全新的 AI 开发工具,定名 AI IDE AIR
java·intellij idea
MX_93597 小时前
SpringMVC请求参数
java·后端·spring·servlet·apache
ID_180079054737 小时前
小红书笔记评论 API,Python 调用示例与完整 JSON 返回参考
java·开发语言
lifewange8 小时前
java连接Mysql数据库
java·数据库·mysql
云原生指北8 小时前
命令行四件套:fd-rg-fzf-bat
java·大数据·elasticsearch