一、 前言 1. 開發過程中經常會使用java將office系列文檔轉換為PDF, 一般都使用微軟提供的openoffice+jodconverter 實現轉換文檔。 2. openoffice既有windows版本也有linux版。不用擔心生產環境是linux系統。 二、 准備 1. 下載安裝openoffice http://www.openoffice.org/ (路徑隨意,因為不管怎么選路徑,咱們想要的都在"C:/Program Files (x86)/OpenOffice 4/",版本建議選最新的) 2. 安裝完事后 cmd 運行 cd C:\Program Files\OpenOffice.org 3\program>soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard (Linux上自己百度,太多了) 3. 准備jar包(下面這些基本上夠了,至於報錯你就在根據報錯的提示下載吧!) commons-cli-1.0.jar commons-io-1.3.1.jar jodconverter-2.2.1.jar jodconverter-cli-2.2.1.jar juh-2.3.0.jar jurt-2.3.0.jar ridl-2.3.0.jar slf4j-api-1.4.3.jar slf4j-jdk14-1.4.3.jar unoil-2.3.0.jar xstream-1.2.2.jar 三、 干活 1. 主要部分 import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; public class WordToPdf { public static int office2PDF(String sourceFile, String destFile) { //String OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";// 這里是OpenOffice的安裝目錄,C:\Program Files (x86)\OpenOffice 4 String OpenOffice_HOME = "C:/Program Files (x86)/OpenOffice 4/"; // 在我的項目中,為了便於拓展接口,沒有直接寫成這個樣子,但是這樣是盡對沒題目的 // 假如從文件中讀取的URL地址最后一個字符不是 '\',則添加'\' if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') { OpenOffice_HOME += "/"; } Process pro = null; try { File inputFile = new File(sourceFile); if (!inputFile.exists()) { return -1;// 找不到源文件, 則返回-1 } // 如果目標路徑不存在, 則新建該路徑 File outputFile = new File(destFile); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } // 啟動OpenOffice的服務 String command = OpenOffice_HOME + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;StarOffice.ServiceManager\" -nofirststartwizard"; pro = Runtime.getRuntime().exec(command); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100); //OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); //converter.convert(inputFile,xml,outputFile,pdf); // close the connection connection.disconnect(); // 封閉OpenOffice服務的進程 pro.destroy(); return 0; } catch (FileNotFoundException e) { e.printStackTrace(); return -1; } catch (IOException e) { e.printStackTrace(); } finally { pro.destroy(); } return 1; } public static void main(String[] args){ // WordToPdf wordToPdf = new WordToPdf(); } } 2. 測試部分 public static void main(String[]args) throws Exception{ String sourceFile = "E:\\zgtzs.doc"; String destFile = "E:\\pdfdest.pdf"; int i = WordToPdf.office2PDF(sourceFile, destFile); System.out.println(i); }
10月12日新增
上面的word轉pdf只限正規的word轉(如果你的Word是通過freemark等xml模板轉過來的,那你轉成pdf后是一對xml代碼,建議你用poi轉代碼如下)
1.所需jar包
dom4j-1.6.1.jar
poi-3.12-20150511.jar
poi-examples-3.12-20150511.jar
poi-excelant-3.12-20150511.jar
poi-ooxml-3.12-20150511.jar
poi-ooxml-schemas-3.12-20150511.jar
poi-scratchpad-3.12-20150511.jar
xmlbeans-2.3.0.jar
2. 代碼
public void testWrite() throws Exception { String templatePath = "E:\\demo\\zgtzs.doc"; //這個是一個doc模板 樣式完全不會亂 InputStream is = new FileInputStream(templatePath); HWPFDocument doc = new HWPFDocument(is); Range range = doc.getRange(); //替換數據(當然如果你要替換的比較多,那建議你寫一個實體類的反射來填充數據) range.replaceText("${top_year}", new SimpleDateFormat("yyyy").format(new Date())); range.replaceText("${content_year1}", new SimpleDateFormat("yyyy").format(new Date())); range.replaceText("${unit}", "北京雲盾科技666"); range.replaceText("${bianHao}", "22"); range.replaceText("${content2}", "asdfasdfasdfassdfasdfasdfaf"); OutputStream os = new FileOutputStream("E:\\demo\\write2.doc"); //把doc輸出到輸出流中 doc.write(os); os.close(); is.close(); }
3. 至於模板我還是截個圖吧!
