long start = System.currentTimeMillis(); ActiveXComponent app = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch();
// filePath word的路径,,尽量使用绝对路径 Dispatch doc = Dispatch.call(docs, "Open", filePath, false, true).toDispatch(); // file 获取的是 当前项目路径。。。保存的地址
filePath = file + "\\wordTemplate\\"+ "商标申请合同.pdf"; File tofile = new File(filePath); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc, "SaveAs", filePath, DocUtil.getWdFormatPDF()); Dispatch.call(doc, "Close", false); long end = System.currentTimeMillis(); } catch (Exception e) { System.out.println("Word转PDF出错:" + e.getMessage()); } finally { if (app != null) { app.invoke("Quit", DocUtil.getWdDoNotSaveChanges()); } }
注意 这里jacob要放在 jdk,bin和 jre bin 和 system32 里各一份,注意下载的是64还是32位。(具体另行研究。)