package com.merge.rui; import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; public class Office2Pdf { public static void main(String[] args) { String filename = "C:\\Users\\rui\\Desktop\\test\\Swing.docx"; Word2PDF(filename); } /** * word转pdf,路径+文件名 */ static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。 static final int wdFormatPDF = 17;// PDF 格式 public static void Word2PDF(String filename){ String toFilename = filename.substring(0,filename.indexOf(".")) + ".pdf"; ActiveXComponent app = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); // System.out.println("打开文档..." + filename); Dispatch doc = Dispatch.call(docs,// "Open", // filename,// FileName false,// ConfirmConversions true // ReadOnly ).toDispatch(); // System.out.println("转换文档到PDF..." + toFilename); File tofile = new File(toFilename); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc,// "SaveAs", // toFilename, // FileName wdFormatPDF); Dispatch.call(doc, "Close", false); System.out.println(filename+" convert pdf is done"); // 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); } } }

gshnlj LV15
2022年8月24日
2252536772 LV21
2022年2月18日
thornton2011 LV2
2021年9月2日
aihui523 LV34
2021年3月23日
wupujian LV17
2020年12月30日
18321000850 LV14
2020年6月24日
EdgarLi LV14
2020年5月16日
kkalpha LV5
2020年3月20日
qq371348836 LV7
2020年3月12日
xuyongff LV24
2019年11月4日