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

相关推荐
有梦想的攻城狮10 分钟前
SpEL(Spring Expression Language)使用详解
java·后端·spring·spel
极小狐16 分钟前
如何从极狐GitLab 容器镜像库中删除容器镜像?
java·linux·开发语言·数据库·python·elasticsearch·gitlab
caihuayuan541 分钟前
前端面试2
java·大数据·spring boot·后端·课程设计
黄雪超1 小时前
JVM——Java语法糖与Java编译器
java·开发语言·jvm
旷野本野1 小时前
【JavaWeb+后端常用部件】
java·开发语言
大G哥1 小时前
Rust 之 trait 与泛型的奥秘
java·开发语言·jvm·数据结构·rust
isyangli_blog1 小时前
(1-1)Java的JDK、JRE、JVM三者间的关系
java·开发语言·jvm
Kisorge2 小时前
【PDF】使用Adobe Acrobat dc添加水印和加密
pdf
sword devil9002 小时前
Python实用工具:pdf转doc
python·pdf
zhuiQiuMX2 小时前
笔试阶段性心得总结
java·python