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 }