OpenOffice2PDF,利用openoffice实现将word和excel转为pdf
2013-03-10 17:13
393 查看
package com.xfzx.test.POI.main; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.regex.Pattern; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; public class OpenOffice2PDF { /** * office中各种格式 */ private static final String[] OFFICE_POSTFIXS = { "doc", "docx", "xls", "xlsx", "ppt", "pptx" }; private ArrayList<String> Office_Formats = new ArrayList<String>(); /** * pdf格式 */ private static final String PDF_POSTFIX= "pdf"; /** * 根据操作系统的名称,获取OpenOffice.org 3的安装目录 如我的OpenOffice.org 3安装在:C:/Program * Files/OpenOffice.org 3 */ public String getOfficeHome() { String osName = System.getProperty("os.name"); if (Pattern.matches("Linux.*", osName)) { return "/opt/openoffice.org3"; } else if (Pattern.matches("Windows.*", osName)) { return "D:/Program Files/OpenOffice.org 3"; } return null; } /** * 转换文件 * @param inputFilePath * @param outputFilePath * @param converter */ public void converterFile(String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) { File inputFile=new File(inputFilePath); File outputFile = new File(outputFilePath); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } converter.convert(inputFile, outputFile); System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!"); } /** * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件 * * @param inputFilePath * 源文件路径,如:"e:/test.docx" * @param outputFilePath * 如果指定则按照指定方法,如果未指定(null)则按照源文件路径自动生成目标文件路径,如:"e:/test_docx.pdf" * @return */ public boolean openOffice2Pdf(String inputFilePath, String outputFilePath) { boolean flag = false; /* * 连接OpenOffice.org 并且启动OpenOffice.org */ DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 获取OpenOffice.org 3的安装目录 String officeHome = getOfficeHome(); config.setOfficeHome(officeHome); // 启动OpenOffice的服务 OfficeManager officeManager = config.buildOfficeManager(); officeManager.start(); // 连接OpenOffice OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); long begin_time = new Date().getTime(); File inputFile=new File(inputFilePath); Collections.addAll(Office_Formats, OFFICE_POSTFIXS); if ((null != inputFilePath) && (inputFile.exists())) { // 判断目标文件路径是否为空 if (Office_Formats.contains(getPostfix(inputFilePath))) { if (null == outputFilePath) { // 转换后的文件路径 String outputFilePath_new = generateDefaultOutputFilePath(inputFilePath); converterFile(inputFilePath, outputFilePath_new, converter); flag = true; } else { converterFile(inputFilePath, outputFilePath, converter); flag = true; } } } else { System.out.println("con't find the resource"); } long end_time = new Date().getTime(); System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms"); officeManager.stop(); return flag; } /** * 如果未设置输出文件路径则按照源文件路径和文件名生成输出文件地址。例,输入为 D:/fee.xlsx 则输出为D:/fee_xlsx.pdf */ public String generateDefaultOutputFilePath(String inputFilePath) { String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), "_" + getPostfix(inputFilePath) + ".pdf"); return outputFilePath; } /** * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx" */ public String getPostfix(String inputFilePath) { String[] p = inputFilePath.split("\\."); if (p.length > 0) {// 判断文件有无扩展名 // 比较文件扩展名 return p[p.length - 1]; } else { return null; } } public static void main(String[] args) { OpenOffice2PDF office2pdf = new OpenOffice2PDF(); office2pdf.openOffice2Pdf("D:/国家知识产权局第二届运动会拔河及田进比赛的通知.doc", "D:/国家知识产权局第二届运动会拔河及田进比赛的通知_" + new Date().getTime() + "." + PDF_POSTFIX); office2pdf.openOffice2Pdf("D:/函件自定义调用字段_20130220_GC.xls",null); } }
相关文章推荐
- 将Office(如:Word、Excel、PPT 等)文件转PDF(通过OpenOffice实现)
- java利用jacob实现word,ppt,excel,jpg转pdf
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
- Java利用OpenOffice进行将word,excel转化成html解析到界面(下)
- openoffice4.1.3+jquery.media.js 实现txt,doc,excel,ppt,pdf在线预览
- Java程序实现Word文档转为pdf以及出现的问题解决
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Java利用OpenOffice将word等office文档转换成PDF
- 将PDF和图片文件如何转为Word或Excel分享几种转换器软件方法
- 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出
- 利用FlexPaper实现Word、PPT、PDF在线浏览
- java实现word,ppt,excel,jpg转pdf
- 利用Excel对Word文档实现自动填充
- java实现将文件(包含word,excel,图片等)转PDF
- PHP 实现Word,excel等转换pdf
- 利用OpenOffice将word转换成PDF
- java实现导出excel、word、 pdf
- JS利用ActiveX控件的方式,即 Excel.Application,实现对word或者excel的打印
- C#实现 word、pdf、ppt 转为图片
- CentOS 7 安装OpenOffice并实现WordToPDF(Java调用)