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); } } }