下载SaveAsPDFandXPS插件
下载jacob.jar
先安装SaveAsPDFandXPS ,安装成功后 , 打开jacob的jar包 ,将jacob.jar放到项目的第三方依赖文件下,如lib。
将
jacob-1.17-x64.dll
jacob-1.17-x86.dll
放到C:\Windows\System32和 jre目录bin的里面
我的是win10系统,Office2016,Java8版本,代码参考如下:
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
private static final int wdFormatPDF = 17;// 指定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:\\新