读书人

一个老有关问题jacob关闭WORD进程(

发布时间: 2012-12-25 16:18:28 作者: rapoo

一个老问题,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,



String destinPDFFilePath) {

if (!openWord(sourceFilePath)) {

closeWord(true);

return;

}

//建立Adobe Distiller的com对象

ActiveXComponent distiller = new ActiveXComponent(
"PDFDistiller.PDFDistiller.1");

try {

//设置当前使用的打印机,我的Adobe Distiller打印机名字为 "Adobe PDF"

wordCom.setProperty("ActivePrinter", new Variant("Adobe PDF"));

//设置printout的参数,将word文档打印为postscript文档。现在只使用了前5个参数,假如要使用更多的话可以参考MSDN的office开发相关api

//是否在后台运行

Variant Background = False;

//是否追加打印

Variant Append = False;

//打印所有文档

int wdPrintAllDocument = 0;

Variant Range = new Variant(wdPrintAllDocument);

//输出的postscript文件的路径

Variant OutputFileName = new Variant(destinPSFilePath);

Dispatch.callN( (Dispatch) wordDoc, "PrintOut", new Variant[] {

Background, Append, Range, OutputFileName});

System.out.println("由word文档打印为ps文档成功!");

//调用Distiller对象的FileToPDF方法所用的参数,具体内容参考Distiller Api手册

//作为输入的ps文档路径

Variant inputPostScriptFilePath = new Variant(destinPSFilePath);

//作为输出的pdf文档的路径

Variant outputPDFFilePath = new Variant(destinPDFFilePath);

//定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件

Variant PDFOption = new Variant("");

//调用FileToPDF方法将ps文档转换为pdf文档

Dispatch.callN(distiller, "FileToPDF", new Variant[] {

inputPostScriptFilePath, outputPDFFilePath, PDFOption});

System.out.println("由ps文档转换为pdf文档成功!");

}
catch (Exception ex) {

ex.printStackTrace();

}
finally {

closeWord(true);

}

}

public static void main(String[] argv) {

WordToPdf d2p = new WordToPdf();


d2p.docToPDF("D:\\test.doc", "D:\\test.ps", "D:\\test.pdf");

boolean success = (new File("D:\\test.ps")).delete();

if (success) {

System.out.println("删除打印机文件成功");

}

}

}

都在windows 系统上跑。
在我家里机器测试(WIN XP),WORD进程关闭了
在我公司机器上WORD进程也关闭了(WIN SERVER 2003)。
在公司测试服务器上对一个WORD重新操作就会关闭不了进程(WIN SERVER 2003)。
我想调用下面这句话来换着操作关闭文档,jbuilder总是报编译错误。
Dispatch.call(wordDoc,"Close",new Variant(0));

"WordToPdf.java": cannot resolve symbol: method call (java.lang.Object,java.lang.String,com.jacob.com.Variant)in class com.jacob.com.Dispatch at line 76, column 10

本来要发布程序使用了,因为进程不能关闭的关系只能延期了。若是能


请各位指导指导啊。急啊。谢谢!




[最优解释]
楼主, 你用jacob做过修改上传的word文件吗?
[其他解释]
补充一下:
在公司测试服务器上,关闭的方法也执行了,只是有个确认框提示是否需要保存。
本想调用Dispatch.call(wordDoc,"Close",new Variant(0));直接关闭WORD文档,但编译不过。

不知上面代码问题还是机器问题?请各位指教啊。
若是能直接关闭最好(但那句话编译不过);若能够加一句选择“否”不保存的语句关闭也可以啊。

谢谢!




[其他解释]

引用:
楼主, 你用jacob做过修改上传的word文件吗?

没有。
应该能google到代码吧。
[其他解释]
没人看?
分不够我可以加啊
[其他解释]
http://hi.baidu.com/mingfang0219/blog/item/cc964e51786bb9501038c205.html
[其他解释]
不知道你的问题解决了没?我也遇到了同样的问题,经过仔细检查我发现是声明wordDoc的时候声明错了,应该将声明private Object wordDoc = null;修改为private Dispatch wordDoc = null;照这样做我的问题解决了,希望对你有帮助!
[其他解释]
不知道lz的问题解决了没?我也遇到了,郁闷很。其实jacob的版本和jdk的版本都有影响的,jacob1.17和jdk1.6不会出问题。

读书人网 >Java相关

热点推荐