本文實例為大家分享了java利用jacob.jar將word轉pdf的具體代碼,供大家參考,具體內容如下
1.jacob.jar配置說明
jacob 就是 java-com bridge的縮寫,提供自動化的訪問com的功能,使用jacob.jar首先電腦要安裝了office。
將jacob.jar jacob.jar導入到項目lib目錄使用前,還要然后把jacob.bll放入c:\windows\system32目錄下,同時還要放入java/jdk/jre/bin目錄下(選擇bll文件的時候,如果是32位就選86,64位選64)。
2.程序代碼
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
|
package core.util; import java.io.file; import com.jacob.activex.activexcomponent; import com.jacob.com.dispatch; public class word2pdf { static final int wddonotsavechanges = 0 ; // 不保存待定的更改。 static final int wdformatpdf = 17 ; // pdf 格式 public static void wordtopdf(string wordpath,string pdfpath) { system.out.println( "啟動word..." ); long start = system.currenttimemillis(); activexcomponent app = null ; try { //打開word應用程序 app = new activexcomponent( "word.application" ); ////設置應用操作是文檔不在明面上顯示,只在后臺靜默處理。 app.setproperty( "visible" , false ); //獲得文檔集合,用來操作我們需要處理的文檔. dispatch docs = app.getproperty( "documents" ).todispatch(); system.out.println( "打開文檔..." + wordpath); //打開word文檔 dispatch doc = dispatch.call(docs, // "open" , // wordpath, // filename false , // confirmconversions true // readonly ).todispatch(); system.out.println( "轉換文檔到pdf..." + pdfpath); file tofile = new file(pdfpath); //創建存放pdf的文件夾 if (tofile.exists()) { tofile.delete(); } //將word另存為pdf dispatch.call(doc, // "saveas" , // pdfpath, // filename wdformatpdf); //關閉word文檔 dispatch.call(doc, "close" , false ); long end = system.currenttimemillis(); system.out.println( "轉換完成..用時:" + (end - start) + "ms." ); } catch (exception e) { system.out.println( "========error:文檔轉換失敗:" + e.getmessage()); } finally { if (app != null ) app.invoke( "quit" , wddonotsavechanges); } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/huanshirenjian/article/details/46407979