找个兼职做网站的,贵阳软件开发公司排名,线上销售的方法和技巧,山东东营市有几个区县一、多余的话
java实现word转pdf可用的jar包不多#xff0c;很多都是收费的。最近发现com.documents4j挺好用的#xff0c;它支持在本机转换#xff0c;也支持远程服务转换。但它依赖于微软的office。电脑需要安装office才能转换。鉴于没在linux中使用office#xff0c;本…一、多余的话
java实现word转pdf可用的jar包不多很多都是收费的。最近发现com.documents4j挺好用的它支持在本机转换也支持远程服务转换。但它依赖于微软的office。电脑需要安装office才能转换。鉴于没在linux中使用office本文转换在windows中进行。
用途主要是对word文件转换成pdf后提供在线预览服务。也可以用于合同生成等。 二、前提条件
windows服务器或电脑需安装office软件。
三、代码实现
添加依赖 dependencygroupIdcom.documents4j/groupIdartifactIddocuments4j-local/artifactIdversion1.1.6/version/dependencydependencygroupIdcom.documents4j/groupIdartifactIddocuments4j-transformer-msoffice-word/artifactIdversion1.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 inputStreamnull;OutputStream outputStream null;IConverter converter getConverter();try {inputStream new FileInputStream(inputFile);outputStream new FileOutputStream(outputFile);String wordFilePath_lowfromFilePath.toLowerCase();if (wordFilePath_low.endsWith(.docx)) {FutureBoolean 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)){FutureBoolean 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)){FutureBoolean 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(FutureBoolean schedule, int timeout){int time0;while (!schedule.isDone()){MyThread.sleep(500);time500;if(timetimeout){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蓝何忠 emaillanhezhong163.com ***********************************************************************************************