本文研究的主要是Java編程利用openoffice將doc、docx轉(zhuǎn)為pdf的實現(xiàn)代碼,具體如下。
1. 需要用的軟件
OpenOffice , JodConverter
2.啟動OpenOffice的服務(wù)
我到網(wǎng)上查如何利用OpenOffice進行轉(zhuǎn)碼的時候,都是需要先用cmd啟動一個soffice服務(wù),啟動的命令是:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"。
但是實際上,對于我的項目,進行轉(zhuǎn)碼只是偶爾進行,然而當(dāng)OpenOffice的轉(zhuǎn)碼服務(wù)啟動以后,該進程(進程名稱是soffice.exe)會一直存在,并且大約占100M的內(nèi)存,感覺非常浪費。于是我就想了一個辦法,可以將執(zhí)行該服務(wù)的命令直接在Java代碼里面調(diào)用,然后當(dāng)轉(zhuǎn)碼完成的時候,直接干掉這個進程。在后面的JAVA代碼里面會有解釋。
所以,實際上,這第2步可以直接跳過
3.將JodConverter相關(guān)的jar包添加到項目中
將JodConverter解壓縮以后,把lib下面的jar包全部添加到項目中
注意:安裝openoffice
4. 下面就是重點嘍,詳見Java代碼解析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
package cn; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; 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; /** * office轉(zhuǎn)化為pdf * pdf轉(zhuǎn)化為swf文件 * @author Administrator * */ public class Converter { private static String openOfficePath = "E:\\安裝軟件\\openoffice\\date" ; //openoffice軟件的安裝路徑 /** * 將Office文檔轉(zhuǎn)換為PDF. 運行該函數(shù)需要用到OpenOffice和jodconverter-2.2.2 * <pre> * 方法示例: * String sourcePath = "F:\\office\\source.doc"; * String destFile = "F:\\pdf\\dest.pdf"; * Converter.office2PDF(sourcePath, destFile); * </pre> * * @param sourceFile * 源文件, 絕對路徑. 可以是Office2003-2007全部格式的文檔, Office2010的沒測試. 包括.doc, * .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc * @param destFile * 目標(biāo)文件. 絕對路徑. 示例: F:\\pdf\\dest.pdf * @return 操作成功與否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置錯誤; 如果返回 0, * 則表示操作成功; 返回1, 則表示轉(zhuǎn)換失敗 */ public static int office2PDF(String sourceFile, String destFile) { try { File inputFile = new File(sourceFile); if (!inputFile.exists()) { return - 1 ; // 找不到源文件, 則返回-1 } // 如果目標(biāo)路徑不存在, 則新建該路徑 File outputFile = new File(destFile); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } String OpenOffice_HOME = openOfficePath; //這里是OpenOffice的安裝目錄 // 如果從文件中讀取的URL地址最后一個字符不是 '\',則添加'\' if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1 ) != '\\' ) { OpenOffice_HOME += "\\" ; } // 啟動OpenOffice的服務(wù) String command = OpenOffice_HOME + "program\\soffice.exe -headless -accept=\"socket,host= 127.0 . 0.1 ,port= 8100 ; urp; \ "" ; Process pro = Runtime.getRuntime().exec(command); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection( "127.0.0.1" , 8100 ); connection.connect(); // convert DocumentConverter converter = new OpenOfficeDocumentConverter( connection); converter.convert(inputFile, outputFile); // close the connection connection.disconnect(); // 關(guān)閉OpenOffice服務(wù)的進程 pro.destroy(); return 0 ; } catch (FileNotFoundException e) { e.printStackTrace(); return - 1 ; } catch (IOException e) { e.printStackTrace(); } return 1 ; } public static void main(String []args) throws Exception { String sourcePath = "C:\\Users\\Administrator\\Desktop\\1\\分組情況一覽表.xls" ; String destFile = "C:\\Users\\Administrator\\Desktop\\1\\dest.pdf" ; int flag = Converter.office2PDF(sourcePath, destFile); if (flag == 1 ) { System.out.println( "轉(zhuǎn)化失敗" ); } else if (flag == 0 ){ System.out.println( "轉(zhuǎn)化成功" ); } else { System.out.println( "找不到源文件, 或url.properties配置錯誤" ); } } } |
總結(jié)
以上就是本文關(guān)于Java利用openoffice將doc、docx轉(zhuǎn)為pdf實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/freewindgo/article/details/54693027