本文实例为大家分享了java使用jacob将微软office文档转成pdf的具体代码,供大家参考,具体内容如下
在使用jacb前,我们需要去下载 jacob.jar 和 jacob-1.18-x64.dll
其次,我们需要将jacob-1.18-x64.dll放入到jdk的bin目录下才可以使用
第三,使用jacb之前,我们需要确保office能正常使用
如果你现在使用的是maven工程,那么不好意思,现在还没有发布正式的jacb资源文件,我们需要自定的maven依赖,如下:
1
2
3
4
5
6
7
|
<dependency> <groupid>com.jacob</groupid> <artifactid>jacob</artifactid> <version> 1.7 </version> <scope>system</scope> <systempath>${basedir}/../fileconvertapp/src/main/webapp/web-inf/lib/jacob.jar</systempath> </dependency> |
然后需要注意的是jar的地址,需要根据自己的情况修改
接下来我们贴一下具体使用的代码片段
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
import java.io.file; import org.apache.log4j.logger; import com.jacob.activex.activexcomponent; import com.jacob.com.dispatch; import com.jacob.com.variant; /** * converter util * * @author jason * */ public class officeconverterutil { /** * log */ private static logger logger = logger.getlogger(officeconverterutil. class ); private static final int wdfo_rmatpdf = 17 ; private static final int xltype_pdf = 0 ; private static final int ppt_saveas_pdf = 32 ; public static final int word_html = 8 ; public static final int word_txt = 7 ; public static final int excel_html = 44 ; public static final int ppt_saveas_jpg = 17 ; // private static final int msotrue = -1; // private static final int msofalse = 0; /** * @param arginputfilepath * @param argpdfpath * @return */ public static boolean officefileconvertertopdf(string arginputfilepath, string argpdfpath) { if (arginputfilepath.isempty() || argpdfpath.isempty() || getfilesufix(arginputfilepath).isempty()) { logger.debug( "输入或输出文件路徑有誤!" ); return false ; } string suffix = getfilesufix(arginputfilepath); file file = new file(arginputfilepath); if (!file.exists()) { logger.debug( "文件不存在!" ); return false ; } // pdf如果不存在则创建文件夹 file = new file(getfilepath(argpdfpath)); if (!file.exists()) { file.mkdir(); } // 如果输入的路径为pdf 则生成失败 if (suffix.equals( "pdf" )) { system.out.println( "pdf not need to convert!" ); return false ; } if (suffix.equals( "doc" ) || suffix.equals( "docx" ) || suffix.equals( "txt" )) { return wordtopdf(arginputfilepath, argpdfpath); } else if (suffix.equals( "xls" ) || suffix.equals( "xlsx" )) { return exceltopdf(arginputfilepath, argpdfpath); } else if (suffix.equals( "ppt" ) || suffix.equals( "pptx" )) { return ppttopdf(arginputfilepath, argpdfpath); // return ppt2pdf(arginputfilepath, argpdfpath); } return false ; } /** * converter word to pdf * * @param wordpath * @param pdfpath * @return */ public static boolean wordtopdf(string wordpath, string pdfpath) { activexcomponent mswordapp = new activexcomponent( "word.application" ); mswordapp.setproperty( "visible" , new variant( false )); dispatch docs = dispatch.get(mswordapp, "documents" ).todispatch(); // long pdfstart = system.currenttimemillis(); dispatch doc = dispatch.invoke(docs, "open" , dispatch.method, new object[] { wordpath, new variant( false ), new variant( true ) }, new int [ 1 ]).todispatch(); deletepdf(pdfpath); dispatch.invoke(doc, "saveas" , dispatch.method, new object[] { pdfpath, new variant(wdfo_rmatpdf) }, new int [ 1 ]); // long pdfend = system.currenttimemillis(); logger.debug(wordpath + ",pdf转换完成.." ); if ( null != doc) { dispatch.call(doc, "close" , false ); } return true ; } /** * excel to pdf * * @param inputfile * @param pdffile * @return */ public static boolean exceltopdf(string inputfile, string pdffile) { activexcomponent activexcomponent = new activexcomponent( "excel.application" ); activexcomponent.setproperty( "visible" , false ); deletepdf(pdffile); dispatch excels = activexcomponent.getproperty( "workbooks" ).todispatch(); dispatch excel = dispatch.call(excels, "open" , inputfile, false , true ).todispatch(); dispatch.call(excel, "exportasfixedformat" , xltype_pdf, pdffile); dispatch.call(excel, "close" , false ); activexcomponent.invoke( "quit" ); return true ; } /** * ppt to pdf * * @param inputfile * @param pdffile * @return */ public static boolean ppttopdf(string inputfile, string pdffile) { // comthread.initsta(); activexcomponent activexcomponent = new activexcomponent( "powerpoint.application" ); // activexcomponent.setproperty("visible", new variant(false)); dispatch ppts = activexcomponent.getproperty( "presentations" ).todispatch(); deletepdf(pdffile); dispatch ppt = dispatch.call(ppts, "open" , inputfile, false , // readonly true , // untitled指定文件是否有标题 true // withwindow指定文件是否可见 ).todispatch(); // dispatch ppt = dispatch.invoke(ppts, "open", dispatch.method, new object[] { inputfile, new variant(false), new variant(true) }, new int[1]).todispatch(); // dispatch.call(ppt, "saveas", pdffile, ppt_saveas_pdf); // dispatch.call(ppt, "saveas", pdffile, new variant(ppt_saveas_pdf)); // dispatch.call(ppt, "saveas", pdffile, new variant(ppt_saveas_pdf)); // dispatch.invoke(ppt, "saveas", dispatch.method, new object[] { pdffile, ppt_saveas_pdf }, new int[1]); // dispatch.invoke(ppt, "saveas", dispatch.method, new object[] { new variant(ppt_saveas_pdf) }, new int[1]); dispatch.calln(ppt, "saveas" , new variant(pdffile)); dispatch.call(ppt, "close" ); activexcomponent.invoke( "quit" ); // comthread.release(); return true ; } /** * ppt to img * * @param inputfile * @param imgfile * @return */ public static boolean ppttoimg(string inputfile, string imgfile) { // 打开word应用程序 activexcomponent app = new activexcomponent( "powerpoint.application" ); // 设置word不可见,office可能有限制 // app.setproperty("visible", false); // 获取word中国所打开的文档,返回documents对象 dispatch files = app.getproperty( "presentations" ).todispatch(); // 调用documents对象中open方法打开文档,并返回打开的文档对象document dispatch file = dispatch.call(files, "open" , inputfile, true , true , false ).todispatch(); // 调用document对象的saveas方法,将文档保存为pdf格式 // dispatch.call(doc, "exportasfixedformat", outputfile, // ppt_to_pdf); dispatch.call(file, "saveas" , imgfile, ppt_saveas_jpg); // 关闭文档 // dispatch.call(file, "close", false); dispatch.call(file, "close" ); // 关闭word应用程序 // app.invoke("quit", 0); app.invoke( "quit" ); return true ; } /** * get file extension * * @param argfilepath * @return */ public static string getfilesufix(string argfilepath) { int splitindex = argfilepath.lastindexof( "." ); return argfilepath.substring(splitindex + 1 ); } /** * substring file path * * @param argfilepath * file path * @return filepaths */ public static string getfilepath(string argfilepath) { int pathindex = argfilepath.lastindexof( "/" ); return argfilepath.substring( 0 , pathindex); } /** * 如果pdf存在则删除pdf * * @param pdfpath */ private static void deletepdf(string pdfpath) { file pdffile = new file(pdfpath); if (pdffile.exists()) { pdffile.delete(); } } } |
根据自己的调试,试验一下吧。
另外还有一段wps转pdf,也贴一下,供大家参考一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public void wps2pdf(string inputfile,string pdffile) { file sfile = new file(inputfile); file tfile = new file(pdffile); activexcomponent wps = null ; try { comthread.initsta(); wps = new activexcomponent( "wps.application" ); activexcomponent doc = wps.invokegetcomponent( "documents" ).invokegetcomponent( "open" , new variant(sfile.getabsolutepath())); doc.invoke( "exportpdf" , new variant(tfile.getabsolutepath())); doc.invoke( "close" ); doc.saferelease(); } catch (exception e) { system.out.println(e.getmessage()); } finally { if (wps != null ) { wps.invoke( "terminate" ); wps.saferelease(); } comthread.release(); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/jesionsly/article/details/51728218