word转pdf的java实现(documents4j)

embedded/2024/9/23 3:09:13/

一、多余的话

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

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

支持文件格式:docx,doc,txt

二、前提条件

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/embedded/39467.html

相关文章

[排序算法]基数排序

目录 1.基本思想 2.基数排序的步骤 LSD 基数排序的基本步骤&#xff1a; 3.基数排序算法的实现 4.时间复杂度分析 5.总结 PS&#xff1a;如有错漏之处&#xff0c;敬请指正 1.基本思想 基数排序&#xff08;Radix Sort&#xff09;是一种非比较性的整数排序算法&#x…

SpringBoot项目配置HTTPS接口的安全访问

参考&#xff1a;https://blog.csdn.net/weixin_45355769/article/details/131727935 安装好openssl后&#xff0c; 创建 D:\certificate CA文件夹下包含&#xff1a; index.txt OpenSSL在创建自签证书时会向该文件里写下索引database.txt OpenSSL会模拟数据库将一些敏感信息…

攻略:ChatGPT3.5~4.0(中文版)国内无限制免费版(附网址)【2024年5月最新更新】

一、什么是ChatGPT&#xff1f; 1、ChatGPT的全名是Chat Generative Pre-trained Transformer&#xff0c;其中"chat"表示聊天。"GPT"则是由三部分组成&#xff1a;生成式&#xff08;generative&#xff09;意味着具有创造力&#xff1b;预训练&#xff0…

【强训笔记】day19

NO.1 代码实现&#xff1a; #include <iostream> using namespace std;int gcd(int x,int y) {if(y0) return x;return gcd(y,x%y); } int main() {int n,a;while(cin>>n>>a){int b;for(int i0;i<n;i){cin>>b;if(a>b) ab;else agcd(a,b);}cou…

Vue从入门到实战Day03

一、生命周期 1. 生命周期四个阶段 思考&#xff1a; ①什么时候可以发送初始化渲染请求&#xff1f; 答&#xff1a;越早越好&#xff0c;在创建阶段后 ②什么时候可以开始操作DOM&#xff1f; 答&#xff1a;至少DOM得渲染出来&#xff0c;在挂载阶段结束后。 Vue生命周…

鸿蒙内核源码分析(时间管理篇) | 谁是内核基本时间单位

时间概念太重要了&#xff0c;在鸿蒙内核又是如何管理和使用时间的呢? 时间管理以系统时钟 g_sysClock 为基础&#xff0c;给应用程序提供所有和时间有关的服务。 用户以秒、毫秒为单位计时.操作系统以Tick为单位计时&#xff0c;这个认识很重要. 每秒的tick大小很大程度上决…

[力扣题解]37. 解数独

题目&#xff1a;37. 解数独 思路 回溯法 代码 class Solution { public:bool function(vector<vector<char>>& board){int i, j;char k;for(i 0; i < 9; i){for(j 0; j < 9; j){// 为空if(board[i][j] .){for(k 1; k < 9; k){if(right(board…

C 语言中怎么产生真正的随机数?

在C语言中&#xff0c;要产生真正的随机数&#xff0c;我们通常使用标准库中的 <stdlib.h> 头文件中提供的随机数生成函数。 这些函数可以生成伪随机数&#xff0c;但它们在一定程度上是随机的&#xff0c;足以满足大多数应用程序的需求。 1. 伪随机数生成函数 C标准库…