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

做网站标配互联网营销推广渠道

做网站标配,互联网营销推广渠道,做图模板网站有哪些,做微商想做个网站一、多余的话 java实现word转pdf可用的jar包不多,很多都是收费的。最近发现com.documents4j挺好用的,它支持在本机转换,也支持远程服务转换。但它依赖于微软的office。电脑需要安装office才能转换。鉴于没在linux中使用office,本…

一、多余的话

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.khdw.cn/news/41773.html

相关文章:

  • 网站怎么做定时任务短链接生成网址
  • 为什么有的网站打不开怎样做好服务营销
  • 美容美发网站建设方案潍坊seo建站
  • 网站导航是什么电商运营培训课程有哪些
  • 合肥建网站的公司公司企业网站模板
  • 中国万网怎么自己做网站windows优化大师免费版
  • 网站自然排名怎么优化seo网站优化方
  • 四川省住房和城乡建设厅考试报名seo网站内部优化方案
  • 武汉做网站开发的公司百度热搜榜历史
  • 手机之家中关村朝阳seo排名
  • 潮阳发布最新通告优化大师哪个好
  • 想网上卖家具怎么做网站广州市口碑seo推广外包
  • wordpress能连115seo网络推广方法
  • 网站建站服务的公司传媒公司
  • 别人做的网站百度网站验证百度智能建站平台
  • 网页设计建网站流程营销软件排名
  • 网站怎么设置标题关键词分析工具有哪些
  • 药品在哪些网站做推广电脑优化软件排行榜
  • 怎么用hbuilder做网站成都seo优化推广
  • 网站开发费用明细电商seo是什么
  • 制作网站的花多少钱seocui cn
  • 天津做网站公司百度移动权重
  • 黔西南做网站的有几家邯郸seo优化公司
  • 新公司网站建设云建站
  • 毕业设计网站设计十大搜索引擎神器
  • 昌平网站建设浩森宇特小红书关键词热度查询
  • 网站制作吧西安核心关键词排名
  • 怎么架设个人网站谷歌搜索引擎大全
  • 做公司网站写什么信息seo是什么单位
  • 做网站的框架软文是什么东西