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

相关推荐
小梁努力敲代码12 小时前
java数据结构--List的介绍
java·开发语言·数据结构
摸鱼的老谭13 小时前
构建Agent该选Python还是Java ?
java·python·agent
lang2015092813 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
夫唯不争,故无尤也13 小时前
Tomcat 启动后只显示 index.jsp,没有进入你的 Servlet 逻辑
java·servlet·tomcat
zz-zjx13 小时前
Tomcat核心组件全解析
java·tomcat
Deschen13 小时前
设计模式-外观模式
java·设计模式·外观模式
why技术14 小时前
从18w到1600w播放量,我的一点思考。
java·前端·后端
夫唯不争,故无尤也14 小时前
JavaWeb流式传输速查宝典
java·流式传输
苏小瀚15 小时前
算法---位运算
java·算法
Camel卡蒙15 小时前
数据结构——二叉搜索树Binary Search Tree(介绍、Java实现增删查改、中序遍历等)
java·开发语言·数据结构