需要的资源:
注意将jacob-1.19-x64.dll和jacob-1.19-x84.dll放在java安装目录的jre/bin下
示例:E:\Java\jdk1.8.0_111\jre
示例代码:
public class WordToPdf {
private static final int wdFormatPDF = 17;// PDF 格式
public static void wordToPDF(){
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
//转换前的文件路径
String startFile = "F:\\测试.docx";
//转换后的文件路劲
String overFile = "F:\\测试.pdf";
doc = Dispatch.call(docs, "Open" , startFile).toDispatch();
File tofile = new File(overFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,"SaveAs", overFile, wdFormatPDF);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
Dispatch.call(doc,"Close",false);
if (app != null)
app.invoke("Quit", new Variant[] {});
}
//结束后关闭进程
ComThread.Release();
}
public static void main(String[] args) {
wordToPDF();
}
}