import java.io.File; |
|
import org.artofsolving.jodconverter.OfficeDocumentConverter; |
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; |
import org.artofsolving.jodconverter.office.OfficeManager; |
|
/** |
* 这是一个工具类,主要是为了使Office2003-2007全部格式的文档 |
* (.doc|.docx|.xls|.xlsx|.ppt|.pptx)转化为pdf文件 |
*/ |
public class OfficeToPDF { |
|
public static final String OFFICE_DOC = "doc" ; |
public static final String OFFICE_DOCX = "docx" ; |
public static final String OFFICE_XLS = "xls" ; |
public static final String OFFICE_XLSX = "xlsx" ; |
public static final String OFFICE_PPT = "ppt" ; |
public static final String OFFICE_PPTX = "pptx" ; |
public static final String OFFICE_TO_PDF = "pdf" ; |
|
/** |
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br> |
* |
* @param inputFilePath |
* 源文件路径,如:"e:/test.docx" |
* @param outputFilePath |
* 目标文件路径,如:"e:/test_docx.pdf" |
* @return |
*/ |
public static boolean openOfficeToPDF(String inputFilePath, String outputFilePath) { |
return office2pdf(inputFilePath, outputFilePath); |
} |
|
/** |
* 获取OpenOffice.org 3的安装目录 |
* |
* @return OpenOffice.org 3的安装目录 |
*/ |
public static String getOfficeHome() { |
String path = PropertiesUtil.getValueByPropertyName( |
ClassLoader.getSystemResourceAsStream( "toPdf.properties" ), |
"OPENOFFICE_PATH" ); |
return path; |
} |
|
/** |
* 连接OpenOffice.org 并且启动OpenOffice.org |
* |
* @return |
*/ |
public static OfficeManager getOfficeManager() { |
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); |
// 获取OpenOffice.org 3的安装目录 |
String officeHome = getOfficeHome(); |
config.setOfficeHome(officeHome); |
// 启动OpenOffice的服务 |
OfficeManager officeManager = config.buildOfficeManager(); |
officeManager.start(); |
return officeManager; |
} |
|
/** |
* 转换文件 |
* |
* @param inputFile |
* @param outputFilePath_end |
* @param inputFilePath |
* @param outputFilePath |
* @param converter |
*/ |
public static void converterFile(File inputFile, String outputFilePath_end, |
String inputFilePath, String outputFilePath, |
OfficeDocumentConverter converter) { |
File outputFile = new File(outputFilePath_end); |
// 假如目标路径不存在,则新建该路径 |
if (!outputFile.getParentFile().exists()) { |
outputFile.getParentFile().mkdirs(); |
} |
converter.convert(inputFile, outputFile); |
} |
|
/** |
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件 |
* |
* @param inputFilePath |
* 源文件路径,如:"e:/test.docx" |
* @param outputFilePath |
* 目标文件路径,如:"e:/test_docx.pdf" |
* @return |
*/ |
public static boolean office2pdf(String inputFilePath, String outputFilePath) { |
boolean flag = false ; |
OfficeManager officeManager = getOfficeManager(); |
// 连接OpenOffice |
OfficeDocumentConverter converter = new OfficeDocumentConverter( |
officeManager); |
if ( null != inputFilePath) { |
File inputFile = new File(inputFilePath); |
// 判断目标文件路径是否为空 |
if ( null == outputFilePath) { |
// 转换后的文件路径 |
String outputFilePath_end = getOutputFilePath(inputFilePath); |
if (inputFile.exists()) { // 找不到源文件, 则返回 |
converterFile(inputFile, outputFilePath_end, inputFilePath, |
outputFilePath, converter); |
flag = true ; |
} |
} else { |
if (inputFile.exists()) { // 找不到源文件, 则返回 |
converterFile(inputFile, outputFilePath, inputFilePath, |
outputFilePath, converter); |
flag = true ; |
} |
} |
officeManager.stop(); |
} else { |
flag = false ; |
} |
return flag; |
} |
|
/** |
* 获取输出文件 |
* |
* @param inputFilePath |
* @return |
*/ |
public static String getOutputFilePath(String inputFilePath) { |
String outputFilePath = inputFilePath.replaceAll( "." |
+ getPostfix(inputFilePath), ".pdf" ); |
return outputFilePath; |
} |
|
/** |
* 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx" |
* |
* @param inputFilePath |
* @return |
*/ |
public static String getPostfix(String inputFilePath) { |
return inputFilePath.substring(inputFilePath.lastIndexOf( "." ) + 1 ); |
} |