900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 使用Java代码将word execl ppt文件转换为pdf格式

使用Java代码将word execl ppt文件转换为pdf格式

时间:2019-05-09 21:55:09

相关推荐

使用Java代码将word execl ppt文件转换为pdf格式

office文件转换为pdf格式

使用OpenOffice转换前言:通过第三方工具openoffice,将word、excel、ppt等文件转换为pdf文件支持在线 预 览;官网地址:/download/一、安装OpenOffice1、打开运行程序 这里是安装向导首界面 点击下一步按钮2、点击浏览按钮 选择安装目录路径,输入使用的用户 以及选择用户权限 点击安装3、输入使用的用户 以及选择用户权限 点击下一步4、这里勾选自定义安装,点击下一步,如果这里勾选的是 通常 会默认安装到c盘,如果是代码启动,目录要指定到c盘的5、把用不到的功能禁用,并指定到同一目录下方便管理,点击下一步6、继续下一步7、安装完成8、启动服务 打开cmd 切换到安装目录program下(如果组件安装选的是 通常,组件会安装到c盘二、引入jar包三、创建工具类

使用OpenOffice转换

前言:通过第三方工具openoffice,将word、excel、ppt等文件转换为pdf文件支持在线 预 览;官网地址:/download/

一、安装OpenOffice

链接: /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

链接: /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);} }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。