這裏簡單介紹下OpenOffice
OpenOffice.org 是一套跨平臺的辦公室軟件套件,能在Windows、linux、MacOS X (X11)
和 Solaris 等操作系統上執行。它與各個主要的辦公室軟件套件兼容。
OpenOffice.org 是自由軟件,任何人都可以免費下載、使用及推廣它。
需要注意: Java環境下操作OpenOffice 要使用 JodConverter。
需要在服務器中安裝OpenOffice
OpenOffice工具大家可以去官網下載,文件比較大我這裏沒辦法分享
安裝好OpenOffice跟JodConverter的jar包導入後,就開始編碼吧。
下面我直接貼代碼
import java.io.File;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.springframework.util.StringUtils;
/**
* 這是一個工具類,主要是爲了使Office2003-2007全部格式的文檔(.doc|.docx|.xls|.xlsx|.ppt|.pptx)
* 轉化爲pdf文件
* Office2010的沒測試
* @author ZhouMengShun
*/
public class Office2PDF {
/**
* 使Office2003-2007全部格式的文檔(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 轉化爲pdf文件
* @param inputFilePath 源文件路徑,如:"D:/論壇.docx"
* @return
*/
public static File openOfficeToPDF(String inputFilePath) {
return office2pdf(inputFilePath);
}
/**
* 根據操作系統的名稱,獲取OpenOffice.org 4的安裝目錄<br>
* 如我的OpenOffice.org 4安裝在:C:/Program Files (x86)/OpenOffice 4
* @return OpenOffice.org 4的安裝目錄
*/
public static String getOfficeHome() {
//這是返回的是OpenOffice的安裝目錄,建議將這個路徑加入到配置文件中,然後直接通過配置文件獲取
//我這裏就直接寫死了
return "C:/Program Files (x86)/OpenOffice 4";
}
/**
* 連接OpenOffice.org 並且啓動OpenOffice.org
* @return
*/
public static OfficeManager getOfficeManager() {
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
// 設置OpenOffice.org 4的安裝目錄
config.setOfficeHome(getOfficeHome());
// 啓動OpenOffice的服務
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
return officeManager;
}
/**
* 轉換文件
* @param inputFile
* @param outputFilePath_end
* @param inputFilePath
* @param outputFilePath
* @param converter
*/
public static File converterFile(File inputFile,String outputFilePath_end,String inputFilePath,
OfficeDocumentConverter converter) {
File outputFile = new File(outputFilePath_end);
//判斷目標路徑是否存在,如不存在則創建該路徑
if (!outputFile.getParentFile().exists()){
outputFile.getParentFile().mkdirs();
}
converter.convert(inputFile, outputFile);//轉換
System.out.println("文件:"+inputFilePath+"\n轉換爲\n目標文件:"+outputFile+"\n成功!");
return outputFile;
}
/**
* 使Office2003-2007全部格式的文檔(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 轉化爲pdf文件
* @param inputFilePath 源文件路徑,如:"D:/論壇.docx"
* @param outputFilePath 目標文件路徑,如:"D:/論壇.pdf"
* @return
*/
public static File office2pdf(String inputFilePath) {
OfficeManager officeManager = null;
try {
if (StringUtils.isEmpty(inputFilePath)) {
System.out.println("輸入文件地址爲空,轉換終止!");
return null;
}
File inputFile = new File(inputFilePath);
//轉換後的文件路徑
String outputFilePath_end=getOutputFilePath(inputFilePath);
if (!inputFile.exists()) {
System.out.println("輸入文件不存在,轉換終止!");
return null;
}
//獲取OpenOffice的安裝路勁
officeManager = getOfficeManager();
//連接OpenOffice
OfficeDocumentConverter converter=new OfficeDocumentConverter(officeManager);
//轉換並返回轉換後的文件對象
return converterFile(inputFile,outputFilePath_end,inputFilePath,converter);
} catch (Exception e) {
System.out.println("轉化出錯!");
e.printStackTrace();
} finally {
if (officeManager != null) {
//停止openOffice
officeManager.stop();
}
}
return null;
}
/**
* 獲取輸出文件
* @param inputFilePath
* @return
*/
public static String getOutputFilePath(String inputFilePath) {
String outputFilePath=inputFilePath.replaceAll("."+getPostfix(inputFilePath),".pdf");
return outputFilePath;
}
/**
* 獲取inputFilePath的後綴名,如:"D:/論壇.docx"的後綴名爲:"docx"
* @param inputFilePath
* @return
*/
public static String getPostfix(String inputFilePath) {
return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
}
//測試
public static void main(String[] args) {
openOfficeToPDF("D:/論壇.docx");
}
}
轉換後如下: