使用Jacob转Word、Exce、PPT为PDF文件
1.去jacob的官网下载需要的版本
2.将jacob-xx版本-x64.dll(系统为64位)/jacob-xx版本-x86.dll(系统为32位)文件放到jdk安装目录下jre/bin目录中
3.重启开发软件,或重启相关服务
4.系统中必须装有office或WPS软件
package com.test.util;
import com.test.util.DateUtils;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Date;
public class PdfUtil {
private static Logger logger = LoggerFactory.getLogger(PdfUtil.class);
/**
* Word转换成PDF
*
* @param wordPath 目标word的路径
* @param pdfName 目标生成pdf的名字
* @return
*/
public static String WordToPdf(String wordPath, String pdfName) {
logger.info("=============== Word 转换 Pdf 开始 ===============");
File wordFile = new File(wordPath);
if (!wordFile.exists()) {
logger.error("word文件不存在");
return null;
}
ActiveXComponent app = null;
Dispatch document = null;
String pdfpath = "目标PDF的生成路径" + DateUtils.format(new Date(), "yyyyMMdd") + "/";
try {
ComThread.InitSTA(true);
// 打开word
app = new ActiveXComponent("Word.Application");
// 获得word中所有打开的文档
// Dispatch documents = app.getProperty("Documents").toDispatch();
logger.info("&#