word转pdf的java实现(documents4j)

server/2024/9/23 2:21:05/

一、多余的话

java实现word转pdf可用的jar包不多,很多都是收费的。最近发现com.documents4j挺好用的,它支持在本机转换,也支持远程服务转换。但它依赖于微软的office。电脑需要安装office才能转换。鉴于没在linux中使用office,本文转换在windows中进行。

用途:主要是对word文件转换成pdf后,提供在线预览服务。也可以用于合同生成等。

二、前提条件

windows服务器或电脑需安装office软件。

三、代码实现

添加依赖:

        <dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.1.6</version></dependency><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.1.6</version></dependency>

转换代码类:WordToPdfUtil.java

package com.lan.fts.util;import com.documents4j.api.*;
import com.documents4j.job.LocalConverter;import java.io.*;
import java.util.concurrent.Future;public class WordToPdfUtil {private IConverter getConverter(){return LocalConverter.builder().build();}private void releaseConverter(IConverter converter){converter.shutDown();}public boolean wordToPdf(String fromFilePath, String pdfFilePath){boolean result = false;File inputFile = new File(fromFilePath);File outputFile = new File(pdfFilePath);InputStream inputStream=null;OutputStream outputStream = null;IConverter converter = getConverter();try {inputStream = new FileInputStream(inputFile);outputStream = new FileOutputStream(outputFile);String wordFilePath_low=fromFilePath.toLowerCase();if (wordFilePath_low.endsWith(".docx")) {Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.DOCX).to(outputStream, true).as(DocumentType.PDF).schedule();result = waitsShedule(schedule, 180000);}else if(wordFilePath_low.endsWith(".doc")){Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.DOC).to(outputStream, true).as(DocumentType.PDF).schedule();result = waitsShedule(schedule, 180000);}else if(wordFilePath_low.endsWith(".txt")){Future<Boolean> schedule = converter.convert(inputStream, true).as(DocumentType.TEXT).to(outputStream, true).as(DocumentType.PDF).schedule();result = waitsShedule(schedule, 180000);}} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {if(outputStream!=null)outputStream.close();} catch (IOException e) {};try {if(inputStream!=null)inputStream.close();} catch (IOException e) {};releaseConverter(converter);}return result;}private boolean waitsShedule(Future<Boolean> schedule, int timeout){int time=0;while (!schedule.isDone()){MyThread.sleep(500);time+=500;if(time>timeout){schedule.cancel(true);return false;}}return true;}public static void main(String[] args) {//	new WordToPdfUtil().wordToPdf("D:\\data\\out\\ffec88b6ee26397bf99834acb059f7b0.docx", "D:\\data\\out\\ffec88b6ee26397bf99834acb059f7b0.docx.pdf");}}

说明:waitsShedule,是等待转换完成。如果超时,将取消转换任务

四、运行验证

	public static void main(String[] args) {new WordToPdfUtil().wordToPdf("D:\\data\\out\\lanhezhong文件转换.docx", "D:\\data\\out\\lanhezhong文件转换.docx.pdf");}

运行结果:

***********************************************************************************************
author:蓝何忠
email:lanhezhong@163.com
***********************************************************************************************


http://www.ppmy.cn/server/39541.html

相关文章

给网站网页PHP页面设置密码访问代码

将MkEncrypt.php文件上传至你网站根目录下或者同级目录下。 MkEncrypt.php里面添加代码&#xff0c;再将调用代码添加到你需要加密的页进行调用 MkEncrypt(‘123456’);括号里面123456修改成你需要设置的密码。 密码正确才能进去页面&#xff0c;进入后会存下cookies值&…

linux_查看磁盘大小

查看硬盘的使用情况df&#xff0c;-h单元为根据大小适当显示&#xff0c;-m单位为M&#xff1b; adminubuntu-test:~$ df -h 文件系统 容量 已用 可用 已用% 挂载点 udev 7.8G 0 7.8G 0% /dev tmpfs 1.6G 3.6M 1.6G 1% /run /dev/…

安卓提示Cannot resolve symbol ‘BuildConfig‘

安卓提示Cannot resolve symbol BuildConfig build.gradle android {...defaultConfig {...versionName "1.1.2" // 这里设置版本号...}... }java代码使用 tv_version.setText(BuildConfig.VERSION_NAME) ; 提示错误 Cannot resolve symbol BuildConfig 解决办法 bu…

Java 集合-List

集合主要分为两组(单列集合, 双列集合) Connection 接口有两个重要的子接口LIst 和 Set, 它们的实现子类都是单列集合, Map 接口的实现子类是双列集合, 存放的是 K-V Connection 接口 Collection 接口和常用方法 下面以 ArrayList 演示一下 add: 添加单个元素remove: 删除指…

python,预测,微调,融合,强化学习,深度学习,机器学习程序,环境调试

python代做&#xff0c;预测&#xff0c;微调&#xff0c;融合&#xff0c;强化学习&#xff0c;深度学习&#xff0c;机器学习程序代写&#xff0c;环境调试&#xff0c;代码调通&#xff0c;模型优化&#xff0c;模型修改&#xff0c;时间序列&#xff0c;机器学习数据处理等…

Leetcode—1235. 规划兼职工作【困难】(upper_bound、自定义排序规则)

2024每日刷题&#xff08;125&#xff09; Leetcode—1235. 规划兼职工作 算法思想 实现代码 class Solution { public:int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {int n startTime.size();vec…

hw蓝初中级面试题整理(流量特征+场景题)

Webshell检测&#xff1a; 0、d盾河马阿里伏魔查杀1、有具体时间的话可以根据时间和正则匹配关键字定位&#xff0c;然后封ip&#xff0c;追踪ip最后做了什么2、在网络层监控 HTTP&#xff0c;并且检测到有人访问了一个从没访问过的文件&#xff0c;而且返回了 200&#xff0c…

贪心算法----最大数

今日题目&#xff1a;leetcode179------点击跳转题目 分析&#xff1a; 要把这些数组组成最大的数&#xff0c;首先我们把数字转化为字符串&#xff0c;根据自定义的排序规则把这些字符串字数排列&#xff0c;再用一个字符串接受这些字符串数字拼接成最大的字符串数字 排序规则…