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

相关推荐
海梨花1 分钟前
【从零开始学习Redis】项目实战-黑马点评D2
java·数据库·redis·后端·缓存
共享家95271 分钟前
linux-高级IO(上)
java·linux·服务器
橘子郡1239 分钟前
观察者模式和发布订阅模式对比,Java示例
java
指针满天飞9 分钟前
Collections.synchronizedList是如何将List变为线程安全的
java·数据结构·list
Java技术小馆10 分钟前
重构 Controller 的 7 个黄金法则
java·后端·面试
金銀銅鐵31 分钟前
[Java] 以 IntStream 为例,浅析 Stream 的实现
java·后端
曳渔2 小时前
UDP/TCP套接字编程简单实战指南
java·开发语言·网络·网络协议·tcp/ip·udp
伊织code2 小时前
pdfminer.six
python·pdf·图片·提取·文本·pdfminer·pdfminer.six
hqxstudying2 小时前
JAVA项目中邮件发送功能
java·开发语言·python·邮件
最初的↘那颗心2 小时前
Java HashMap深度解析:原理、实现与最佳实践
java·开发语言·面试·hashmap·八股文