java开发_模仿百度文库_OpenOffice2PDF_源码下载

这几天在研究模仿着做类似于百度文库的东西,在这里给大家分享一下我自己做的东西。

由于需要做这样的项目,我查阅了很多资料,最后选定一下方案去做:

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

下载地址:

下载后得到: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

* com.b510.office2pdf; java.io.File; 7 import java.util.Date; 8 import java.util.regex.Pattern; org.artofsolving.jodconverter.OfficeDocumentConverter; 11 import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; 12 import org.artofsolving.jodconverter.office.OfficeManager; * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 16 * 转化为pdf文件<br> 17 * Office2010的没测试<br> 18 * 19 * @date 2012-11-5 xhw 21 * Office2PDF {* office中.doc格式String OFFICE_DOC = “doc”;* office中.docx格式String OFFICE_DOCX = “docx”;* office中.xls格式String OFFICE_XLS = “xls”;* office中.xlsx格式String OFFICE_XLSX = “xlsx”;* office中.ppt格式String OFFICE_PPT = “ppt”;* office中.pptx格式String OFFICE_PPTX = “pptx”;* pdf格式String OFFICE_TO_PDF = “pdf”; main(String[] args) { 55Office2PDF office2pdf = new Office2PDF(); 56office2pdf.openOfficeToPDF(“e:/test.” + OFFICE_DOCX, “e:/test_” + new Date().getTime() + “.” + OFFICE_TO_PDF); 57office2pdf.openOfficeToPDF(“e:/test.” + OFFICE_PPTX, null); 58 }* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br> 62 * inputFilePath 64 *源文件路径,如:”e:/test.docx” outputFilePath 66 *目标文件路径,如:”e:/test_docx.pdf” openOfficeToPDF(String inputFilePath, String outputFilePath) { 70return office2pdf(inputFilePath, outputFilePath); 71 }* 根据操作系统的名称,获取OpenOffice.org 3的安装目录<br> 75 * 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3<br> 76 * OpenOffice.org 3的安装目录 String getOfficeHome() { 80String osName = System.getProperty(“os.name”); 81if (Pattern.matches(“Linux.*”, osName)) { 82return “/opt/openoffice.org3”; 83} else if (Pattern.matches(“Windows.*”, osName)) { 84return “C:/Program Files (x86)/OpenOffice.org 3”; 85} else if (Pattern.matches(“Mac.*”, osName)) { 86return “/Application/OpenOffice.org.app/Contents”; 87 }; 89 }* 连接OpenOffice.org 并且启动OpenOffice.org 93 * OfficeManager getOfficeManager() { 97DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();String officeHome = getOfficeHome();100 config.setOfficeHome(officeHome);OfficeManager officeManager = config.buildOfficeManager();103 officeManager.start();104return officeManager;105 }* 转换文件109 * inputFile outputFilePath_end inputFilePath outputFilePath converter converterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) {117File outputFile = new File(outputFilePath_end);(!outputFile.getParentFile().exists()) {120 outputFile.getParentFile().mkdirs();121 }122 converter.convert(inputFile, outputFile);123System.out.println(“文件:” + inputFilePath + “\n转换为\n目标文件:” + outputFile + “\n成功!”);124 }* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>128 * inputFilePath130 *源文件路径,如:”e:/test.docx” outputFilePath132 *目标文件路径,虚拟主机,如:”e:/test_docx.pdf” office2pdf(String inputFilePath, String outputFilePath) {136boolean flag = false;137OfficeManager officeManager = getOfficeManager();OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);140long begin_time = new Date().getTime();141if (null != inputFilePath) {142File inputFile = new File(inputFilePath);(null == outputFilePath) {String outputFilePath_end = getOutputFilePath(inputFilePath);converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);149flag = true;150 }151} else {converterFile(inputFile, outputFilePath, inputFilePath, outputFilePath, converter);154flag = true;155 }156 }157 officeManager.stop();158} else {159System.out.println(“con’t find the resource”);160 }161long end_time = new Date().getTime();162System.out.println(“文件转换耗时:[” + (end_time – begin_time) + “]ms”);163return flag;164 }* 获取输出文件168 * inputFilePath String getOutputFilePath(String inputFilePath) {173String outputFilePath = inputFilePath.replaceAll(“.” + getPostfix(inputFilePath), “.pdf”);174return outputFilePath;175 }* 获取inputFilePath的后缀名,如:”e:/test.pptx”的后缀名为:”pptx”<br>179 * inputFilePath String getPostfix(String inputFilePath) {184return inputFilePath.substring(inputFilePath.lastIndexOf(“.”) + 1);185 }186 187 },虚拟主机,网站空间出门走好路,出口说好话,出手做好事。

java开发_模仿百度文库_OpenOffice2PDF_源码下载

相关文章:

你感兴趣的文章:

标签云: