用java实现word(docx)转换为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 复制代码
    //word文档替换成pdf文档
    private static void replaceWordToPdf(String wordPath, String pdfPath){
        File inputWord = new File(wordPath);
        File outputFile = new File(pdfPath);
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

注意:需要获取到本地的word文件地址;

相关推荐
代码什么用7 小时前
Spring基础使用
java·后端·spring
Su米苏8 小时前
Spring AI 中MCP 与普通 @Tool的区别
java·人工智能·spring
码云数智-园园8 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
老板一杯拿铁8 小时前
Java 异常体系详解:从字节码原理到最佳实践
java
蜗牛互联网8 小时前
Java 17 HttpClient调用文件转写API的超时与失败回退
java·人工智能·后端
禾小西9 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis
我不是阵雨9 小时前
JDK 21虚拟线程Pinning陷阱:一文拔钉解困
java·开发语言
小卡车5559 小时前
java反射、自定义注解的应用(自定义分页)
java
BLUcoding10 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
yuniko-n10 小时前
【JUC】Lock 和 synchronzied 锁
java