OpenOffice2PDF,利用openoffice实现将word和excel转为pdf

  1 package com.xfzx.test.POI.main;
  2 
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 import java.util.Collections;
  6 import java.util.Date;
  7 import java.util.regex.Pattern;
  8 
  9 import org.artofsolving.jodconverter.OfficeDocumentConverter;
 10 import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
 11 import org.artofsolving.jodconverter.office.OfficeManager;
 12 
 13 public class OpenOffice2PDF {
 14 
 15     /**
 16      * office中各种格式
 17      */
 18     private static final String[] OFFICE_POSTFIXS = { "doc", "docx", "xls",
 19             "xlsx", "ppt", "pptx" };
 20     private ArrayList<String> Office_Formats = new ArrayList<String>();
 21 
 22     /**
 23      * pdf格式
 24      */
 25     private static final String PDF_POSTFIX= "pdf";
 26     
 27     /**
 28      * 根据操作系统的名称,获取OpenOffice.org 3的安装目录 如我的OpenOffice.org 3安装在:C:/Program
 29      * Files/OpenOffice.org 3
 30      */
 31     
 32     public String getOfficeHome() {
 33         String osName = System.getProperty("os.name");
 34         if (Pattern.matches("Linux.*", osName)) {
 35             return "/opt/openoffice.org3";
 36         } else if (Pattern.matches("Windows.*", osName)) {
 37             return "D:/Program Files/OpenOffice.org 3";
 38         }
 39         return null;
 40     }
 41     /**
 42      * 转换文件
 43      * @param inputFilePath
 44      * @param outputFilePath
 45      * @param converter
 46      */
 47     public void converterFile(String inputFilePath, String outputFilePath,
 48             OfficeDocumentConverter converter) {
 49         File inputFile=new File(inputFilePath);
 50         File outputFile = new File(outputFilePath);
 51         // 假如目标路径不存在,则新建该路径
 52         if (!outputFile.getParentFile().exists()) {
 53             outputFile.getParentFile().mkdirs();
 54         }
 55         converter.convert(inputFile, outputFile);
 56         System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile
 57                 + "\n成功!");
 58     }
 59 
 60     /**
 61      * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
 62      * 
 63      * @param inputFilePath
 64      *            源文件路径,如:"e:/test.docx"
 65      * @param outputFilePath
 66      *            如果指定则按照指定方法,如果未指定(null)则按照源文件路径自动生成目标文件路径,如:"e:/test_docx.pdf"
 67      * @return
 68      */
 69     public boolean openOffice2Pdf(String inputFilePath, String outputFilePath) {
 70         boolean flag = false;
 71         /*
 72          * 连接OpenOffice.org 并且启动OpenOffice.org
 73          */
 74         DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
 75         // 获取OpenOffice.org 3的安装目录
 76         String officeHome = getOfficeHome();
 77         config.setOfficeHome(officeHome);
 78         // 启动OpenOffice的服务
 79         OfficeManager officeManager = config.buildOfficeManager();
 80         officeManager.start();
 81         // 连接OpenOffice
 82         OfficeDocumentConverter converter = new OfficeDocumentConverter(
 83                 officeManager);
 84         long begin_time = new Date().getTime();
 85         File inputFile=new File(inputFilePath);
 86         Collections.addAll(Office_Formats, OFFICE_POSTFIXS);
 87         if ((null != inputFilePath) && (inputFile.exists())) {
 88             // 判断目标文件路径是否为空
 89             if (Office_Formats.contains(getPostfix(inputFilePath))) {
 90                 if (null == outputFilePath) {
 91                     // 转换后的文件路径
 92                     String outputFilePath_new = generateDefaultOutputFilePath(inputFilePath);
 93                     converterFile(inputFilePath, outputFilePath_new, converter);
 94                     flag = true;
 95 
 96                 } else {
 97                     converterFile(inputFilePath, outputFilePath, converter);
 98                     flag = true;
 99                 }
100             }
101 
102         } else {
103             System.out.println("con't find the resource");
104         }
105         long end_time = new Date().getTime();
106         System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms");
107         officeManager.stop();
108         return flag;
109     }
110 
111     /**
112      * 如果未设置输出文件路径则按照源文件路径和文件名生成输出文件地址。例,输入为 D:/fee.xlsx 则输出为D:/fee_xlsx.pdf
113      */
114     public String generateDefaultOutputFilePath(String inputFilePath) {
115         String outputFilePath = inputFilePath.replaceAll("."
116                 + getPostfix(inputFilePath), "_" + getPostfix(inputFilePath)
117                 + ".pdf");
118         return outputFilePath;
119     }
120 
121     /**
122      * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"
123      */
124     public String getPostfix(String inputFilePath) {
125         String[] p = inputFilePath.split("\\.");
126         if (p.length > 0) {// 判断文件有无扩展名
127             // 比较文件扩展名
128             return p[p.length - 1];
129         } else {
130             return null;
131         }
132     }
133     
134     public static void main(String[] args) {
135 
136         OpenOffice2PDF office2pdf = new OpenOffice2PDF();
137         office2pdf.openOffice2Pdf("D:/国家知识产权局第二届运动会拔河及田进比赛的通知.doc",
138                 "D:/国家知识产权局第二届运动会拔河及田进比赛的通知_" + new Date().getTime() + "."
139                         + PDF_POSTFIX);
140         office2pdf.openOffice2Pdf("D:/函件自定义调用字段_20130220_GC.xls",null);
141     }
142 
143 
144 }

 

posted @ 2013-03-10 17:13  郑小明  阅读(16150)  评论(0编辑  收藏  举报