- 使用jacob-1.7来进行转换,下载jar及dll文件
- jar包放入maven仓库 repository\com\jacob\jacob\1.17\jacob.jar
- jacob-1.17-M2-x64.dll和jacob-1.17-M2-x86 放到两个位置
- C:\Windows\SysWOW64(我是64位系统)
- C:\Program Files\Java\jdk1.8.0_191\jre\bin
- maven仓库存放jacob.jar包的文件夹执行代码:
- mvn install:install-file -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.17 -Dfile=jacob.jar -Dpackaging=jar
运行结果,多了个文件:jacob-1.17.jar
4、pom.xml引入:
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.17</version>
</dependency>
- 测试
package com.lnsoft.docx;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public class PDFUtil {
private static final int wdFormatPDF = 17;
public static void main(String[] args) {
PDFUtil.word2PDF("D:/1.doc", "D:/test11.pdf");
System.out.println("转换完成!");
}
public static boolean word2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
.toDispatch();
Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);
Dispatch.call(doc, "Close", false);
app.invoke("Quit", 0);
return true;
} catch (Exception e) {
return false;
}
}
}