用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文件地址;

相关推荐
孙克旭_19 分钟前
单链表进阶实操:5 道常考面试题详细解析【Java 实现】
java·开发语言·数据结构·单链表
2601_9619017025 分钟前
SpringCloud2023集成Nacos2.4.3
java
玄昌盛不会编程26 分钟前
LeetCode——2091. 从数组中移除最大值和最小值
java·算法·leetcode
Rain的Java大神之路29 分钟前
如何保证接口幂等
java·经验分享·后端·面试·架构
吴声子夜歌30 分钟前
Java——数组和集合(一)
java·开发语言
Jesse_EC32 分钟前
一个看不见的字符——CRLF 如何让我的 Docker 初始化脚本"静默失踪"
java·后端
深念Y32 分钟前
Nuxt 项目 Docker 构建:从 pnpm+node 迁移到全 bun
java·前端·docker
学长毕业设计37 分钟前
基于SpringBoot的社区鲜奶订购系统的设计与实现(源码+文档+讲解视频)
java·spring boot·后端
呆呆敲代码的小Y42 分钟前
【游戏开发】C# 中的迭代器
java·开发语言·c#·迭代器
TinyMemory43 分钟前
Java 面向对象核心入门(六):this 关键字完全解析
java·笔记·面向对象·java新手·this关键字