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

相关推荐
圆山猫8 分钟前
[Virtualization](四):Linux KVM/RISC-V 的 vCPU 运行路径
java·linux·risc-v
城管不管44 分钟前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
IT小白杨1 小时前
从环境制备到自动化工作流:多账号运营的工程化架构拆解
java·经验分享·自动化·安全架构·指纹浏览器
豆瓣鸡1 小时前
算法日记 - Day3
java·开发语言·算法
萧瑟余晖2 小时前
Java深入解析篇九之NIO详解
java·网络·nio
The Chosen One9852 小时前
高进度算法模板速记(待完善)
java·前端·算法
极光代码工作室4 小时前
基于SpringBoot的课程预约系统
java·springboot·web开发·后端开发
Leighteen5 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言
名字还没想好☜5 小时前
Go 的 time.After 在 select 循环里内存泄漏:定时器堆积原理与 timer.Reset 正确姿势
java·数据库·golang·go·goroutine
圆山猫6 小时前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v