下载jacob,位置如下:
jacob.jar 放在 D:\java_1.8_jdk\lib
jacob-1.18-x64.dll和jacob-1.18-x86.dll放在 D:\java_1.8_jdk\bin下载office 2007版的,2003版的和2007版的不能同时使用,有时候会因为不存在插件 SaveAsPDFandXPS.exe 而不能正常运行
下面是两种方式:
(一)
package com.word.test;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class WordToPdf {
static final int wdFormatPDF = 17;// PDF 格式 public void wordToPDF(String sfileName,String toFileName){ System.out.println("启动Word..."); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch(); // doc = Dispatch.call(docs, "Open" , sourceFile).toDispatch(); doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); System.out.println("打开文档..." + sfileName); System.out.println("转换文档到PDF..." + toFileName); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } // Dispatch.call(doc, "SaveAs", destFile, 17); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(17) }, new int[1]); long end = System.currentTimeMillis(); System.out.println("转换完成..用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { Dispatch.call(doc,"Close",false); System.out.println("关闭文档"); if (app != null) app.invoke("Quit", new Variant[] {}); } //如果没有这句话,winword.exe进程将不会关闭 ComThread.Release(); } public static void main(String[] args) { WordToPdf d = new WordToPdf(); d.wordToPDF("e:\\aaa.docx", "e:\\aaa.pdf"); }}
(二)
package com.word.test;
import java.io.File; import java.io.IOException; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; public class Word2Pdf { static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。 static final int wdFormatPDF = 17;// word转PDF 格式 public static void main(String[] args) throws IOException { String source1 = "e:\\aaa.docx"; String target1 = "e:\\aaa.pdf"; Word2Pdf pdf = new Word2Pdf(); pdf.word2pdf(source1, target1); } public static boolean word2pdf(String source, String target) { System.out.println("Word转PDF开始启动..."); long start = System.currentTimeMillis(); ActiveXComponent app = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); System.out.println("打开文档:" + source); Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch(); System.out.println("转换文档到PDF:" + target); File tofile = new File(target); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc, "SaveAs", target, wdFormatPDF); Dispatch.call(doc, "Close", false); long end = System.currentTimeMillis(); System.out.println("转换完成,用时:" + (end - start) + "ms"); return true; } catch (Exception e) { System.out.println("Word转PDF出错:" + e.getMessage()); return false; } finally { if (app != null) { app.invoke("Quit", wdDoNotSaveChanges); } } } }