当前位置: 首页 > news >正文

word转pdf的java实现(documents4j)

一、多余的话

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.lryc.cn/news/345580.html

相关文章:

  • 基于K8S构建Jenkins持续集成平台
  • PHPStudy 访问网页 403 Forbidden禁止访问
  • 热爱电子值得做的电子制作实验
  • .class文件启动过程以及文件内容结构讲解
  • 解锁楼宇自动化新维度西门子Insight+BACnet IP I/O控制器
  • 2024.05.10作业
  • 基于POSIX标准库的读者-写者问题的简单实现
  • 重生我是嵌入式大能之串口调试UART
  • 【智能优化算法】蜜獾优化算法(Honey Badger Algorithm,HBA)
  • 【算法与数据结构】数组
  • 【数据结构】队列详解(Queue)
  • Baumer工业相机堡盟工业相机如何通过NEOAPISDK获取相机的Statistics图像传输统计信息(C#)
  • FreeRTOS标准库例程代码
  • wandb: - 0.000 MB of 0.011 MB uploaded持续出现的解决方案
  • 分布式模式让业务更高效、更安全、更稳定
  • 5.11学习记录
  • Java类加载器介绍
  • VC++ PDH/性能计数器
  • C++ 类和对象:面向对象编程基础
  • linux 基础命令使用
  • eve 导入linux
  • vivado新版本兼容老版本,vitis classic兼容sdk教程
  • 02.02.返回倒数第k个节点
  • MongoDB 从部署到掌握
  • electron-vite工具打包后通过内置配置文件动态修改接口地址实现方法
  • 每日一练2024.5.9
  • P2622 关灯问题
  • 从头开始的建材类电商小程序开发指南
  • 数据结构中的栈(C语言版)
  • (贪心05) 无重叠区间 划分字母区间 合并区间