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

相关推荐
wok1574 小时前
IDEA 无法识别 OkHttpClient?cannot resolve symbol问题解决
java·ide·intellij-idea
吴声子夜歌4 小时前
Java——标准序列化机制
java·序列化
hughnz4 小时前
下一代地热能的技术障碍
java·大数据·数据库
Devin~Y4 小时前
大厂Java面试实录:Spring Boot + JVM + Redis/Kafka + 微服务治理 + Spring AI/RAG 一条龙
java·jvm·spring boot·redis·spring cloud·kafka·openfeign
有梦想的小何4 小时前
Cursor AI 编程实战(篇三):Domain、Infrastructure 与策略模式
java·ai编程·策略模式
西凉的悲伤4 小时前
java通过url获取 jpg、png、pdf 文件格式
java
SunnyDays10114 小时前
Java 实现 PDF 附件的添加与删除:四种实用方法
java·pdf·附件
小小尚@4 小时前
AI 加持!Adobe Acrobat DC 2026 解锁 PDF 高效办公新体验
人工智能·pdf
phltxy4 小时前
Redis 持久化机制
java·redis·git
竹之月4 小时前
【Auto CAD 2020】单张打印输出PDF图纸A0、A1尺寸,黑白颜色
经验分享·pdf·auto cad2020