一个老问题,jacob关闭WORD进程(有的机器可以,有的不行)
google来的一段用jacob把WORD转PDF的代码,:),具体如下:
package demo;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.*;
/*
* 注意word转pdf要安装虚拟打印机,且要配置
* 使用jacob框架,把dll文件放到jre/bin目录下
*/
public class WordToPdf {
private ActiveXComponent wordCom = null;
private Object wordDoc = null;
private final Variant False = new Variant(false);
private final Variant True = new Variant(true);
/**
* 打开word文档
*
* @param filePath word文档
* @return 返回word文档对象
*/
public boolean openWord(String filePath) {
//建立ActiveX部件
wordCom = new ActiveXComponent("Word.Application");
try {
//返回wrdCom.Documents的Dispatch
Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
//调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc
wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,
new Object[] {filePath}
, new int[1]).toDispatch();
return true;
}
catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
/**
* 关闭word文档
*/
public void closeWord(boolean saveOnExit) {
if (wordCom != null) {
//关闭word文件
/*****************************
Dispatch.call(wordDoc,"Close",new Variant(0));
******************************/
wordCom.invoke("Quit",new Variant[0]);
wordCom = null;
//释放在程序线程中引用的其它com,比如Adobe PDFDistiller
ComThread.Release();
}
}
/**
* 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件
*
* @param sourceFilePath
* 源文件路径
* @param destinPSFilePath
* 首先生成的PS文件路径
* @param destinPDFFilePath
* 生成PDF文件路径
*/
public void docToPDF(String sourceFilePath, String destinPSFilePath,