第一步:office转pdf
Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览 今天就完成第一步: Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter) 做之前,我们要先做一些准备: 1.下载:Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe 下载地址:http://www.openoffice.org/download/index.html
下载后得到:Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe 2.安装Apache_OpenOffice 双击Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe进行安装操作
注意:这里的安装位置,要在项目中用到....我安装在:C:/Program Files (x86)/OpenOffice.org 3目录下面
到这里,OpenOffice就算是安装完成了。 3.创建一个项目
/Office2PDF/src/com/b510/office2pdf/Office2PDF.java 1 /** 2 * 3 */ 4 package com.b510.office2pdf; 5 6 import java.io.File; 7 java.util.Date; 8 java.util.regex.Pattern; 9 10 org.artofsolving.jodconverter.OfficeDocumentConverter; 11 org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; 12 org.artofsolving.jodconverter.office.OfficeManager; 13 14 15 * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 16 * 转化为pdf文件<br> 17 * Office2010的没测试<br> 18 19 * @date 2012-11-5 20 * @author xhw 21 22 23 public class Office2PDF { 24 25 26 * office中.doc格式 27 28 static final String OFFICE_DOC = "doc"; 29 30 * office中.docx格式 31 32 final String OFFICE_DOCX = "docx" 33 34 * office中.xls格式 35 36 final String OFFICE_XLS = "xls" 37 38 * office中.xlsx格式 39 40 final String OFFICE_XLSX = "xlsx" 41 42 * office中.ppt格式 43 44 final String OFFICE_PPT = "ppt" 45 46 * office中.pptx格式 47 48 final String OFFICE_PPTX = "pptx" 49 50 * pdf格式 51 52 final String OFFICE_TO_PDF = "pdf" 53 54 void main(String[] args) { 55 Office2PDF office2pdf = new Office2PDF(); 56 office2pdf.openOfficeToPDF("e:/test." + OFFICE_DOCX,"e:/test_" + new Date().getTime() + "." + OFFICE_TO_PDF); 57 office2pdf.openOfficeToPDF("e:/test." + OFFICE_PPTX,null); 58 } 59 60 61 * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br> 62 * 63 * @param inputFilePath 64 * 源文件路径,如:"e:/test.docx" 65 outputFilePath 66 * 目标文件路径,如:"e:/test_docx.pdf" 67 @return 68 69 boolean openOfficeToPDF(String inputFilePath,String outputFilePath) { 70 return office2pdf(inputFilePath,outputFilePath); 71 72 73 74 * 根据操作系统的名称,获取OpenOffice.org 3的安装目录<br> 75 * 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3<br> 76 77 @return OpenOffice.org 3的安装目录 78 79 public String getOfficeHome() { 80 String osName = System.getProperty("os.name" 81 if (Pattern.matches("Linux.*",osName)) { 82 return "/opt/openoffice.org3" 83 } else if (Pattern.matches("Windows.*" 84 return "C:/Program Files (x86)/OpenOffice.org 3" 85 } if (Pattern.matches("Mac.*" 86 return "/Application/OpenOffice.org.app/Contents" 87 } 88 return 89 90 91 92 * 连接OpenOffice.org 并且启动OpenOffice.org 93 94 95 96 OfficeManager getOfficeManager() { 97 DefaultOfficeManagerConfiguration config = DefaultOfficeManagerConfiguration(); 98 // 获取OpenOffice.org 3的安装目录 99 String officeHome = getOfficeHome(); 100 config.setOfficeHome(officeHome); 101 启动OpenOffice的服务 102 OfficeManager officeManager = config.buildOfficeManager(); 103 officeManager.start(); 104 officeManager; 105 106 107 108 * 转换文件 109 110 inputFile 111 outputFilePath_end 112 113 114 converter 115 116 converterFile(File inputFile,String outputFilePath_end,String inputFilePath,String outputFilePath,OfficeDocumentConverter converter) { 117 File outputFile = File(outputFilePath_end); 118 假如目标路径不存在,则新建该路径 119 if (!outputFile.getParentFile().exists()) { 120 outputFile.getParentFile().mkdirs(); 121 122 converter.convert(inputFile,outputFile); 123 System.out.println("文件:" + inputFilePath + "n转换为n目标文件:" + outputFile + "n成功!"124 125 126 127 128 129 130 131 132 133 134 135 office2pdf(String inputFilePath,128)">136 boolean flag = false137 OfficeManager officeManager = getOfficeManager(); 138 连接OpenOffice 139 OfficeDocumentConverter converter = OfficeDocumentConverter(officeManager); 140 long begin_time = Date().getTime(); 141 if (null != inputFilePath) { 142 File inputFile = File(inputFilePath); 143 判断目标文件路径是否为空 144 null == outputFilePath) { 145 转换后的文件路径 146 String outputFilePath_end = getOutputFilePath(inputFilePath); 147 if (inputFile.exists()) { 找不到源文件,则返回 148 converterFile(inputFile,outputFilePath_end,inputFilePath,outputFilePath,converter); 149 flag = true150 } 151 } else { 152 153 154 flag = 155 156 } 157 officeManager.stop(); 158 } 159 System.out.println("con't find the resource"160 161 long end_time = 162 System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms"163 flag; 164 165 166 167 * 获取输出文件 168 169 170 171 172 String getOutputFilePath(String inputFilePath) { 173 String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath),".pdf"174 outputFilePath; 175 176 177 178 * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br> 179 180 181 182 183 String getPostfix(String inputFilePath) { 184 return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1185 186 187 } 4.效果
5.控制台效果 1 十一月 05,2012 5:19:15 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager <init>
2 INFO: ProcessManager implementation is PureJavaProcessManager
3 十一月 05,2012 5:19:15 下午 org.artofsolving.jodconverter.office.OfficeProcess start
4 INFO: starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir 'C:UsersADMINI~1AppDataLocalTemp.jodconverter_socket_host-127.0.0.1_port-2002'
5 十一月 05,128)"> 6 INFO: started process
7 十一月 05,2012 5:19:16 下午 org.artofsolving.jodconverter.office.OfficeConnection connect
8 INFO: connected: 'socket,tcpNoDelay=1'
9 十一月 05,2012 5:19:19 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop
10 INFO: stopping
11 文件:e:/test.docx
12 转换为
13 目标文件:e:test_1352107155307.pdf
14 成功!
15 十一月 05,2012 5:19:19 下午 org.artofsolving.jodconverter.office.OfficeConnection$1 disposing
16 INFO: disconnected: 'socket,128)">17 十一月 05,0)"> 下午 org.artofsolving.jodconverter.office.ManagedOfficeProcess doEnsureProcessExited
18 INFO: process exited with code 0
19 十一月 05,128)">20 INFO: stopped
21 文件转换耗时:[2838]ms
22 十一月 05,2012 5:19:19 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager <init>
23 24 十一月 05,128)">25 INFO: starting process with acceptString 'socket,128)">26 十一月 05,128)">27 28 十一月 05,2012 5:19:2029 INFO: connected: 'socket,128)">30 文件:e:/test.pptx
31 32 目标文件:e:test.pdf
33 34 十一月 05,2012 5:19:2635 36 十一月 05,2012 5:19:26 下午 org.artofsolving.jodconverter.office.OfficeConnection$137 INFO: disconnected: 'socket,128)">38 十一月 05,128)">39 INFO: process exited with code 0
40 十一月 05,128)">41 42 文件转换耗时:[6417]ms
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |