Java:OpenOffice方式实现Word转pdf/html/htm
2019独角兽企业重金招聘Python工程师标准>>>
原本的想法想要直接在页面上实现预览,包括预览样式等等,类似这位博主这种方式:
http://blog.csdn.net/lbf5210/article/details/50519190 但是发现其中的flowpaper貌似只有exe文件可下载,另外就是安装太多插件,麻烦。所以想了下,只要生成文件就是了,不采用其中的预览插件,这样就简单得多了。但必须要安装OpenOffice服务,方可实现转换。
思路:传入已doc/docx文件流对象(不管后缀很方便,一次性处理) -> OpenOfficeConnection建立连接 -> DocumentConverter对象实现转换 -> 关闭OpenOfficeConnection连接。
其实实现过程也很简单,我这里写的工具类可以根据传入的文档的后缀及想要生成的文件格式来自动处理,
其实关键代码就几行而已,如下:
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docInputFile, htmlOutputFile);connection.disconnect();
其他都是细节问题,同样贴出完整代码demo,看注释即可:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;public class OfficeParseUtil {private static OfficeParseUtil officeParseUtil;/*** 描述:实例化* @return* OfficeParseUtil*/public static synchronized OfficeParseUtil getOfficeParseUtil(){if (officeParseUtil == null) {officeParseUtil = new OfficeParseUtil();}return officeParseUtil;}/*** 描述:转换office文件* @param fromFileInputStream 文件* @param toFilePath 保存地址* @param fileName 文件完整名称,带后缀(如:doc docx xls ppt)* @param type 转换类型,如:pdf html htm* @return* @throws IOException* String*/public String parseOffice(InputStream fromFileInputStream, String toFilePath,String fileName,String type) throws IOException {String timesuffix = fileName.substring(1,fileName.indexOf(".")-1);//截取相同相同文件名String postfix = fileName.substring(fileName.indexOf(".")+1);//截取文件后缀String docFileName = null;String htmFileName = null;if("doc".equals(postfix)){docFileName = "doc_" + timesuffix + ".doc";htmFileName = "doc_" + timesuffix + "."+type;}else if("docx".equals(postfix)){docFileName = "docx_" + timesuffix + ".docx";htmFileName = "docx_" + timesuffix + "."+type;}else if("xls".equals(postfix)){docFileName = "xls_" + timesuffix + ".xls";htmFileName = "xls_" + timesuffix + "."+type;}else if("ppt".equals(postfix)){docFileName = "ppt_" + timesuffix + ".ppt";htmFileName = "ppt_" + timesuffix + "."+type;}else{return null;}File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);File docInputFile = new File(toFilePath + File.separatorChar + docFileName);if (!new File(toFilePath).exists()) {docInputFile.getParentFile().mkdirs();}if (htmlOutputFile.exists())htmlOutputFile.delete();htmlOutputFile.createNewFile();if (docInputFile.exists())docInputFile.delete();docInputFile.createNewFile();/*** 由fromFileInputStream构建输入文件*/try {OutputStream os = new FileOutputStream(docInputFile);int bytesRead = 0;byte[] buffer = new byte[1024 * 8];while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {os.write(buffer, 0, bytesRead);}os.close();fromFileInputStream.close();} catch (IOException e) {}OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try {connection.connect();// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docInputFile, htmlOutputFile);connection.disconnect();} catch (ConnectException e) {htmFileName = null;//转换出错删除临时文件htmlOutputFile.delete();System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");}finally{// 转换完之后删除word文件docInputFile.delete();}return htmFileName;}public static void main(String[] args) throws IOException {OfficeParseUtil officeParseUtil = new OfficeParseUtil();File file = null;FileInputStream fileInputStream = null;file = new File("F:/wordtest/xxxxxx.doc");fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");officeParseUtil.parseOffice(fileInputStream, "F:/wordtest/pdf/","白蚁防治协议.doc","pdf");}}
注意:
1、启动OpenOffice服务服务。
2、这里不局限转换成pdf,也可转换成html 、 htm页面
3、转换后html/htm 的样式没有pdf的效果好,例如协议文件最后的签名存在排版有错乱
4、对文档中的Tab缩进的样式兼容也并不好
5、“一、 二、”使用这种Office自带样式标题会变成“1、 2、”
感谢 http://blog.csdn.net/yjclsx/article/details/51445546
转载于:https://my.oschina.net/u/2428684/blog/842947
Java:OpenOffice方式实现Word转pdf/html/htm相关推荐
- java openoffice_Java:OpenOffice方式实现Word转pdf/html/htm
原本的想法想要直接在页面上实现预览,包括预览样式等等,类似这位博主这种方式: http://blog.csdn.net/lbf5210/article/details/50519190 但是发现其中 ...
- Java使用XDOC实现word,pdf等格式的生成及转换
Java使用XDOC实现word,pdf等格式的生成及转换 最近业务上有个需求,要将生成的简报以word或者pdf等格式下载下来 刚开始我设想的是用io流的方法直接写,但是这样无疑是比较麻烦的 直到我 ...
- Java使用aopse实现word转换pdf
Java使用aopse实现word转换pdf 需要引用aspose包,引入操作我写了一个博客,地址如下 https://blog.csdn.net/weixin_46713508/article/de ...
- java 使用jacob实现word转pdf
java 使用jacob实现word转pdf(IDEA Maven项目) 步骤: 一. 插件与jar包下载 SaveAsPDFandXPS 下载地址: http://www.microsoft.com ...
- Java实现Excel和word转pdf加水印,复制及可用
Java实现Excel和word转pdf加水印,复制及可用 时隔大半年,之前朋友,今天突然找我说,他们有个需求要做这东西.想起来之前自己照着官方文档写,也走了不少坑.今天就索性写个,也为了方便以后使用 ...
- 用JAVA实现简单的WORD转PDF 亲测有效
前阵子由于项目业务的需要,领导要求研究一下如何实现将word转换成pdf,经过一番的百度找到了一个比较简单的方法(本人比较懒),只需要导入一个JAR包和十几行代码就能够实现转换,而且转换的效果比预想的 ...
- Java通过IText导出word和pdf
最近做的项目中需要用到导出word和pdf的功能(还有图表),在网上找了很多资料,最后敲定用了IText组件,下面是我项目中的一个Demo,记录了一下,希望对需要的人有帮助. 相关jar包下载地址:h ...
- Java工具类实现word转pdf结果几乎一模一样
Background [封装好的工具类][转换效果99%][无水印] 实现技术[Aspose] 这里给出需要的依赖包 aspose-words-15.8.0.jar和word-license.xml, ...
- java通过aspose实现word转pdf,加水印
引入jar <!-- word转pdf--><dependency><groupId>com.aspose.words</groupId><art ...
最新文章
- Split字符串分割函数
- base64编码_几分钟看懂Base64编码原理
- 无埋点数据收集和adb monkey测试屏蔽通知栏
- 初始序列为1 8 6 2 5 4 7 3一组数采用堆排序,当建堆(小根堆)完毕时,堆所对应的二叉树中序遍历序列为
- python决策树id3算法_python实现决策树ID3算法
- A+B Problem(洛谷-P1001)
- 联机手写汉字识别,基于新型RNN网络结构的方法
- 7-12 两个数的简单计算器 (10 分)
- spgwr | R语言与地理加权回归(Ⅰ-2):广义线性地理加权回归
- html做table某一列的合计,Jquery、js计算table列合计
- fx系列微型可编程控制器 通信_西门子系列资料(64本电子书+PLC视频+软件),整理了很久...
- 计算机语言基础入门百度云,汇编计算机语言入门教程
- 经典与科技碰撞之美!CGU 2018九段卡皇耀世登场
- cesium entity使用
- IPad分屏,当电脑第二显示屏
- 混改中光城投集团控股有限公司 合作方案是如何完成的?
- 深入理解Java虚拟机(周志明第三版)- 第十一章:后端编译与优化
- 有关WEB前端中的语义化
- STM32L152RE实现串口发送及接收数据
- 山东大学软件学院2021区块链考试回忆