使用Java代码将word、execl、ppt文件转换为pdf格式
office文件转换为pdf格式
- 使用OpenOffice转换
- 前言:通过第三方工具openoffice,将word、excel、ppt等文件转换为pdf文件支持在线 预 览;官网地址:http://www.openoffice.org/download/
- 一、安装OpenOffice
- 1、打开运行程序 这里是安装向导首界面 点击下一步按钮
- 2、点击浏览按钮 选择安装目录路径,输入使用的用户 以及选择用户权限 点击安装
- 3、输入使用的用户 以及选择用户权限 点击下一步
- 4、这里勾选自定义安装,点击下一步,如果这里勾选的是 通常 会默认安装到c盘,如果是代码启动,目录要指定到c盘的
- 5、把用不到的功能禁用,并指定到同一目录下方便管理,点击下一步
- 6、继续下一步
- 7、安装完成
- 8、启动服务 打开cmd 切换到安装目录program下(如果组件安装选的是 通常,组件会安装到c盘
- 二、引入jar包
- 三、创建工具类
使用OpenOffice转换
前言:通过第三方工具openoffice,将word、excel、ppt等文件转换为pdf文件支持在线 预 览;官网地址:http://www.openoffice.org/download/
一、安装OpenOffice
链接: https://pan.baidu.com/s/1pTCL-U5TuKLTp4Uz5Fceng
提取码: xhdf
1、打开运行程序 这里是安装向导首界面 点击下一步按钮
2、点击浏览按钮 选择安装目录路径,输入使用的用户 以及选择用户权限 点击安装
如图所示
3、输入使用的用户 以及选择用户权限 点击下一步
4、这里勾选自定义安装,点击下一步,如果这里勾选的是 通常 会默认安装到c盘,如果是代码启动,目录要指定到c盘的
5、把用不到的功能禁用,并指定到同一目录下方便管理,点击下一步
6、继续下一步
7、安装完成
8、启动服务 打开cmd 切换到安装目录program下(如果组件安装选的是 通常,组件会安装到c盘
// 粘贴此命令
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
二、引入jar包
commons-cli-1.2.jar
commons-io-1.4.jar
jodconverter-2.2.2.jar
jodconverter-cli-2.2.2.jar
juh-3.0.1.jar
jurt-3.0.1.jar
ridl-3.0.1.jar
slf4j-api-1.5.6.jar
slf4j-jdk14-1.5.6.jar
unoil-3.0.1.jar
xstream-1.3.1.jar
链接: https://pan.baidu.com/s/1CNBA0LM5GAoLIIC-DIPVxA
提取码: 6u3k
三、创建工具类
public class OpenOfficeUtil {private static OpenOfficeUtil doc2HtmlUtil;/*** 获取Doc2HtmlUtil实例*/public static synchronized OpenOfficeUtil getDoc2HtmlUtilInstance() {if (doc2HtmlUtil == null) {doc2HtmlUtil = new OpenOfficeUtil();}return doc2HtmlUtil;}/*** 转换文件成html* @param inputStream* @param path 路径* @param type 文件类型* @return* @throws IOException */public String fileToHtml(InputStream inputStream, String path,String type) throws IOException {Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");String timesuffix = sdf.format(date);String docFileName = null;String htmFileName = null;if("doc".equals(type)){docFileName = "doc_" + timesuffix + ".doc";htmFileName = "doc_" + timesuffix + ".html";}else if("docx".equals(type)){docFileName = "docx_" + timesuffix + ".docx";htmFileName = "docx_" + timesuffix + ".html";}else if("xls".equals(type)){docFileName = "xls_" + timesuffix + ".xls";htmFileName = "xls_" + timesuffix + ".html";}else if("ppt".equals(type)){docFileName = "ppt_" + timesuffix + ".ppt";htmFileName = "ppt_" + timesuffix + ".html";}else{return null;}File htmlOutputFile = new File(path + File.separatorChar + htmFileName);File docInputFile = new File(path + File.separatorChar + docFileName);if (htmlOutputFile.exists())htmlOutputFile.delete();htmlOutputFile.createNewFile();if (docInputFile.exists())docInputFile.delete();docInputFile.createNewFile();try {OutputStream os = new FileOutputStream(docInputFile);int bytesRead = 0;byte[] buffer = new byte[1024 * 8];while ((bytesRead = inputStream.read(buffer)) != -1) {os.write(buffer, 0, bytesRead);}os.close();inputStream.close();} catch (IOException e) {e.printStackTrace();}OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try {connection.connect();} catch (ConnectException e) {System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");}// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docInputFile, htmlOutputFile);connection.disconnect();// 转换完之后删除word文件docInputFile.delete();return htmFileName;}/*** 转化文件为pdf* @param originalFile 原文件* @param inputStream* @param path 转化路径* @param type 文件类型* @return* @throws IOException*/public static File fileToPdf(File originalFile, InputStream inputStream, String path, String type) throws IOException {File filedir=new File(path);if(!filedir.exists()){//如果文件夹不存在filedir.mkdir();//创建文件夹}int lastIndex = originalFile.getName().lastIndexOf(".");String fileName = originalFile.getName().substring(0, lastIndex);String docFileName = null;String htmFileName = null;if("doc".equals(type)){docFileName = "doc_" + fileName + ".doc";htmFileName = "doc_" + fileName + ".pdf";}else if("docx".equals(type)){docFileName = "docx_" + fileName + ".docx";htmFileName = "docx_" + fileName + ".pdf";}else if("xls".equals(type)){docFileName = "xls_" + fileName + ".xls";htmFileName = "xls_" + fileName + ".pdf";}else if("xlsx".equals(type)){docFileName = "xlsx_" + fileName + ".xlsx";htmFileName = "xlsx_" + fileName + ".pdf";}else if("ppt".equals(type)){docFileName = "ppt_" + fileName + ".ppt";htmFileName = "ppt_" + fileName + ".pdf";}else if("pptx".equals(type)){docFileName = "pptx_" + fileName + ".pptx";htmFileName = "pptx_" + fileName + ".pdf";}else{return null;}File htmlOutputFile = new File(path + File.separatorChar + htmFileName);File docInputFile = new File(path + File.separatorChar + docFileName);if (htmlOutputFile.exists()){htmlOutputFile.delete();htmlOutputFile.createNewFile();}if (docInputFile.exists()){docInputFile.delete();docInputFile.createNewFile();}OutputStream os = null;try {os = new FileOutputStream(docInputFile);int bytesRead = 0;byte[] buffer = new byte[1024 * 8];while ((bytesRead = inputStream.read(buffer)) != -1) {os.write(buffer, 0, bytesRead);}inputStream.close();} catch (IOException e) {e.printStackTrace();} finally {if(os != null){os.close();}}OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try {connection.connect();} catch (ConnectException e) {e.printStackTrace();System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");try {// 代码中启动openOfficeStart();fileToPdf(originalFile, inputStream, path, type);} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docInputFile, htmlOutputFile);connection.disconnect();// 转换完之后删除word文件docInputFile.delete();return htmlOutputFile;}/*** 命令行启动*/public static void openOfficeStart(){try {// cd /d D:/OpenOffice 4.1.7/OpenOffice 4/program // soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizardProcess exec = Runtime.getRuntime().exec("cmd /c D: && cd D:/OpenOffice 4.1.7/OpenOffice 4/program && soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard");if( exec.waitFor() == 0){System.out.println("OpenOffice服务启动失败。");}else{System.out.println("OpenOffice服务启动成功。");}} catch (Exception e) {e.printStackTrace();}}/*** 将文件转化为Base64字符串* @param file* @return*/public static String getFileBase64(File file){ InputStream in = null; byte[] data = null; try{ in = new FileInputStream(file); data = new byte[in.available()]; in.read(data); }catch (IOException e){ e.printStackTrace(); }finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace(); } }}// Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);}
}
使用Java代码将word、execl、ppt文件转换为pdf格式相关推荐
- msg文件转成html文件,如何将MSG格式的文件转换为PDF格式文件?
三.如何将MSG格式的文件转换为PDF格式的文件? 想要随时查看MSG格式文件的话,格式转换就成为了理想的途径,下面将教大家将MSG格式的文件转换为PDF格式文件三种方式,一起去学习一下吧. 1.借助 ...
- 如何通过WPS将Word(doc、docx)转换为PDF格式的教程方法
如何通过WPS将Word(doc.docx)转换为PDF格式的教程方法 作者:张国军_Suger 开发工具与关键技术:WPS 很多时候,有些朋友们不知道如何来通过WPS来讲Word(d ...
- 如何将CAD文件转换为PDF格式,免费试用
刚刚接触CAD文件,对于文件的格式还不是很会操作,PDF格式我们平时用的多,使用起来更加方便,随时随地都能打开查看,那么我们如何将CAD文件转换为PDF格式?今天和大家分享一种简单的操作方法,并且是免 ...
- 把从中国知网上下载的CAJ文件转换为PDF格式
许多在中国知网上下载下来的文章是CAJ格式的,这些CAJ格式的文章只能使用知网的阅读工具阅读,在打印.复制等方面都很麻烦.下面笔者就教大家如何把CAJ格式文件转换为PDF格式文件. 工具/原料 CAJ ...
- 如何将CAJ格式文件转换为PDF格式
目录 1.打开CAJViewer 2.打印机名称选择如下图所示 3.选择位置保存转换后的PDF文件 4.正在转换--- 5.转换成功 众所众知,知网很多论文下载下来都是caj格式,想要打开就要专门使用 ...
- 在线免费把Markdown格式文件转换为PDF格式
用CSDN的MarkDown编辑器在线转换 CSDN的MarkDown编辑器说实话还是挺好用的. 导出PDF操作步骤,图文配合看: 在MD编辑模式下写好MarkDown文章或者直接把要转换的MarkD ...
- Word文档怎么转换为PDF格式?介绍两种方式
word应该是我们平日里接触最多的文件格式了,浏览编辑起来都很方便,但打印出来的格式却时不时就出错.这时可以将文档转成格式更稳定的PDF文件,那Word怎么转PDF呢?下面就给大家介绍两种方式,一分钟 ...
- OFD文件怎么转PDF?ofd文件转换为pdf格式教程
如果我们电脑里面装有wps软件,ofd文件可以通过WPS软件转换成PDF,以下为WPS转换的步骤说明: 现在已经有了非常好用的在线转换程序www.DataConvert.cn 可以直接处理 2维数组 ...
- java实现在线预览功能(支持xlx,word,ppt,dwg等格式转Pdf)
offoce转pdf文件预览,基于aspose-cad,aspose-cells,aspose-words,aspose-slides实现word,xls,ppt,dwg转pdf文件预览 之前基于op ...
最新文章
- 项目分发系统-expect
- PIX515防火墙配置策略实例
- ThinkPHP下隐藏index.php以及URL伪静态
- 【python图像处理】彩色映射(续篇)
- Python笔记——基本数据结构:列表、元组及字典
- 【ubuntu-qt-dlib】 配置问题 (一) undefined reference to `XPending'
- 【TensorFlow-windows】keras接口——卷积核可视化
- DataGrid的动态绑定问题(二)
- C++/CLI的简单介绍。from维基百科们,http://zh.wikipedia.org/zh-cn/C%2B%2B/CLI
- [备忘]windows下安装PHP环境php.ini-recommended 跟php.ini-dist 的差别之处
- 华为NP课程笔记16-MPLS(上)
- 基于python的学生信息管理系统毕业论文_学生信息管理系统毕业论文
- html页面排版会乱,窗口缩放导致页面排版错乱的解决方法
- 什么是锁?看完你就明白了
- SSH服务,优化,防止暴力破解
- 服务器多网卡多路由策略
- 运放构成的电压跟随器
- hmailserver + afterlogic 搭建webmail
- win7注册表关闭防火墙服务器,怎么样修改注册表来关闭windows防火墙?
- 4K壁纸批量采集,张张精品,全程自动化