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

相关推荐
d***81728 分钟前
解决SpringBoot项目启动错误:找不到或无法加载主类
java·spring boot·后端
ᐇ95921 分钟前
Java集合框架深度实战:构建智能教育管理与娱乐系统
java·开发语言·娱乐
听风吟丶1 小时前
MyBatis 深度实战:从基础映射到企业级性能优化
java·tomcat
shouchaobao2 小时前
免费PDF工具:PDF转Word/Excel/图片+AI总结+合并拆分+OCR识别,多端无广告!
pdf·word·excel
仟濹2 小时前
【Java 基础】面向对象 - 继承
java·开发语言
6***83052 小时前
微服务搭建----springboot接入Nacos2.x
java
likuolei3 小时前
XML 元素 vs. 属性
xml·java·开发语言
自不量力的A同学3 小时前
Spring Boot 4.0.0 正式发布
java·spring boot·后端
d***29243 小时前
【spring】Spring事件监听器ApplicationListener的使用与源码分析
java·后端·spring
5***b973 小时前
解决报错net.sf.jsqlparser.statement.select.SelectBody
java