jar包下载:(链接:https://pan.baidu.com/s/17cToHJGckw3-jOPX_rgHKA 密码:btew)
dll文件下载:(链接: https://pan.baidu.com/s/1OmchVtGy_XCAU9naO05psw 密码: ckhh)
注:dll文件,放在jdk文件下面的bin目录下,web项目添加dll文件后需重新添加jdk,电脑要能够打开office文档的。
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
/**
*
* @title 调用jacob服务 office文档转pdf
*
* @time 2018-2-7 下午4:24:29
*
* @version 1.0
*/
public class Office2Pdf {
public static void main(String[] args) {
word2PDF("f:/2pdf/a_1234.doc", "f:/2pdf/a_word.pdf");
excel2PDF("f:/2pdf/a_1234.xlsx", "f:/2pdf/a_excel.pdf");
ppt2PDF("f:/2pdf/a_1234.ppt", "f:/2pdf/a_ppt.pdf");
}
private static final int wdFormatPDF = 17;
private static final int xlTypePDF = 0;
private static final int ppSaveAsPDF = 32;
/**
* word转pdf
*
* @param inputFile
* @param pdfFile
* @return
*/
public static boolean word2PDF(String inputFile, String pdfFile) {
try {
ComThread.InitSTA();
// 打开word应用程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
// 设置word不可见
app.setProperty("Visible", false);
// 获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
.toDispatch();
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
// word保存为pdf格式宏,值为17
// Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF);
Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);
// 关闭文档
Dispatch.call(doc, "Close", false);
// 关闭word应用程序
app.invoke("Quit", 0);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
ComThread.Release();
}
}
/**
* excel转pdf
*
* @param inputFile
* @param pdfFile
* @return
*/
public static boolean excel2PDF(String inputFile, String pdfFile) {
try {
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("DisplayAlerts", "False");
app.setProperty("Visible", false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
true).toDispatch();
Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
Dispatch.call(excel, "Close", false);
app.invoke("Quit");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
ComThread.Release();
}
}
/**
* ppt转pdf
*
* @param inputFile
* @param pdfFile
* @return
*/
public static boolean ppt2PDF(String inputFile, String pdfFile) {
try {
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent(
"PowerPoint.Application");
// app.setProperty("Visible", msofalse);
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts, "Open", inputFile,
true,// ReadOnly
true,// Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch();
Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);
Dispatch.call(ppt, "Close");
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
} finally {
ComThread.Release();
}
}
}
常用类以及方法
ComThread:com组件管理,用来初始化com线程,释放线程,所以会在操作office之前使用,操作完成再使用。
ActiveXComponent:创建office的一个应用,比如你操作的是word还是excel
Dispatch:调度处理类,封装了一些操作来操作office,里面所有的可操作对象基本都是这种类型,所以jacob是一种链式操作模式,就像StringBuilder对象,调用append()方法之后返回的还是StringBuilder对象
Variant:封装参数数据类型,因为操作office是的一些方法参数,可能是字符串类型,可能是数字类型,虽然都是1,但是不能通过,可以通过Variant来进行转换通用的参数类型,new Variant(1),new Variant("1"),
Dispatch的几种静态方法:这些方法就是要用来操作office的。
•call( )方法:调用COM对象的方法,返回Variant类型值。
•invoke( )方法:和call方法作用相同,但是不返回值。
•get( )方法:获取COM对象属性,返回variant类型值。
•put( )方法:设置COM对象属性。
以上方法中有的有很多重载方法,调用不同的方法时需要放置不同的参数,至于哪些参数代表什么意思,具体放什么值,就需要参考vba代码了,仅靠jacob是无法进行变成的。
Variant对象的toDispatch()方法:将以上方法返回的Variant类型转换为Dispatch,进行下一次链式操作。
1、初始化com线程
使用jacob之前使用下面的语句来初始化com线程,大概意思是打开冰箱门,准备放大象。。。
ComThread.InitSTA();
使用完成后使用下面的语句来关闭现场,大概意思是关上冰箱门。。。
ComThread.Release();
2、创建应用程序对象,设置参数,得到文档集合
操作一个文档之前,我们必须要创建一个应用对应,比如是word还是excel,设置一些文档应用的参数,得到文档集合对象,(大家应该知道word是Documents,excel是WorkBooks)
ActiveXComponent wordApp = new ActiveXComponent("Word.Application");//word
ActiveXComponent wordApp = new ActiveXComponent("Excel.Application");//excel
wordApp.setProperty("Visible", new Variant(false));//设置应用操作是文档不在明面上显示,只在后台静默处理。
3、打开文档
有了文档对象集合,我们就可以来操作文档了,链式操作就此开始:
call方法,调用open方法,传递一个参数,返回一个我们的word文档对象。
Dispatch doc = Dispatch.call(document, "Open",new Variant("D:\\my.doc")).toDispatch();
4、文档另存为pdf
// doc是打开的文档
Dispatch.call(doc, "Save");
5、退出wordapplication
参数有很多个,我们一个都不传,执行完后winword进程关闭
wordApp.invoke("Quit", new Variant[] {});
6、释放com线程
ComThread.Release();