jocob操作word应用.docx
《jocob操作word应用.docx》由会员分享,可在线阅读,更多相关《jocob操作word应用.docx(43页珍藏版)》请在冰点文库上搜索。
jocob操作word应用
一、jacob把word转换成方正ceb文件
1.ceb是方正生产的一种电子文档格式,所以想要转换ceb文件,首先得去下载方正ApabiMakerceb转换软件安装。
下载地址:
2.JACOB是一个JAVA到微软的COM接口的桥梁。
使用JACOB允许任何JVM访问COM对象,从而使JAVA应用程序能够调用COM对象。
如果你要对MSWord、Excel进行处理,JACOB是一个好的选择。
JACOB目前已经成为
sourceforge(project/)
的一个开源项目,本文使用的版本是1.16版本:
下载地址:
3.相关资源包都找到以后,我们就来体验下jacob如何制作ceb文件的神奇之旅吧。
下面文档都是以Myeclipse中建立的实例
第【一】步:
把jacob.jar中的jacob-1.16-M1-x64.dll和jacob-1.16-M1-x86.dll拷贝到C:
\\WINDOWS\\System32或者jdK\\bin下:
第【二】步:
在项目工程中WEB-INF下导入jacob-1.16-M1-x64.dll和jacob-1.16-M1-x86.dll并且把jacob.jar放入lib下。
第【三】步:
整体环境都搭好了,我们来写下实现制作的java代码:
如下:
importcom.jacob.activeX.ActiveXComponent;
import.Dispatch;
import.Variant;
publicclasstest{
publicstaticvoidmain(String[]args){
//第一步:
初始化控件,"CLSID:
50738EED-857A-4271-889C-FD4BF24A3F77"
代表你所安装ceb制作器在windows注册表中生产的progid或CLSID
ActiveXComponentcom=newActiveXComponent("CLSID:
50738EED-857A-4271-889C-FD4BF24A3F77");
Dispatchdisp=(Dispatch)com.getObject();
//第二步:
调用ceb制作器控件中的MakeFileConvert的方法,此方法有两个参数,第一个
为你所需要制作成ceb文件的决定路径,第二个位生产ceb后保存的决定路径。
booleanisok=Dispatch.call(disp,"MakeFileConvert",newVariant("C:
\\tempss\\ftt.doc"),newVariant("C:
\\tempss\\ftt.ceb")).getBoolean();
System.out.println(isok);
}
}
好了,如果后台输出为ture证明ceb制作成功,如果不成功到注册表中查询CEB制作器有没有在注册表中生成CLSID,如果没有人工在注册表中再次注册。
把CEBControler.ocx人工注册到windows注册表中:
注册方法如下
regsvr32后面跟CEBControler.ocx的决定路径就OK了。
二、jacob操作word不能修改和拷贝
通常在我们的系统中,对正文和红文的操作都是word文件,这样下载到客户端,用户是可以编辑和拷贝的,这样对红文的安全性不够。
这种需求尤其在我们电子政务行业系统中尤为突出。
为了解决这一问题我们可以利用word中保护文档功能,对文档嵌入密码保护。
好了,废话少说,我们具体来看下如何实现此功能:
以下功能实现,界定与MicrosoftOffice2003版本。
第【一】步:
我们现来看看如何通过word自身实现文档保护:
如下
任意打开一份word文件,点击【工具】【保护文档】【编辑限制】【是,启动强制保护】【输入保护密码】即OK了
然后你在对word编辑和拷贝看看,是不是发现已经不能对其进行任何操作了!
好!
我们再来看看jacob如何实现这功能;
首先还是准备环境,具体环境搭配依然与ceb制作环境的第2点相同,如果你一开始操作了第一个实例,就不用再搭配任何环境:
第【二】步:
实现代码如下:
importcom.jacob.activeX.ActiveXComponent;
import.Dispatch;
import.Variant;
publicclasstest1{
publicstaticvoidmain(String[]args){
try{
ActiveXComponentword=newActiveXComponent("Word.Application");
word.setProperty("Visible",newVariant(false));//不可见打开word
word.setProperty("AutomationSecurity",newVariant(3));//禁用宏
Dispatchdocuments=word.getProperty("Documents").toDispatch();
Dispatchdoc=Dispatch.call(documents,"Open","C:
\\tempss\\3657.doc").toDispatch();//打开word
Dispatchselection=Dispatch.get(word,"Selection").toDispatch();//选定的范围或插入点
//设置文档保护密码123
StringprotectionType=Dispatch.get(doc,"ProtectionType").toString();
if(protectionType.equals("-1")){
Dispatch.call(doc,"Protect",newVariant(3),newVariant(true),
"123");
}
//关闭word文档
if(doc!
=null){
Dispatch.call(doc,"Save");
Dispatch.call(doc,"Close",newVariant(true));
doc=null;
}
//关闭全部应用
if(word!
=null){
Dispatch.call(word,"Quit");
word=null;
}
selection=null;
documents=null;
}catch(Exceptione){
e.printStackTrace();
}
}
}
好了:
到此对word进行保护就已经结束了,你可以再次打开你保护的word,发现不能对其进行任何编辑了。
当然这种保护的程度还是不够的,一般不建议这样采用。
因为想要破解这种保护文档也很容易。
在此就不介绍,XX一下一大把。
此二功能需求总结:
为了解决政府机关对外发文,通用ceb文件的转换,和解决我们系统中红文下载到客户端不允许用户修改和拷贝需求。
特做此实例
在了解了jacob应用功能的时候,我们来想想他还能作用于什么地方:
个人认为如下:
1.对系统中所有在线打开,需求不允许拷贝和在线编辑的,我们可以用此功能对文件进行保护,不允许资料拷贝。
像我们广告行业就有这种例子。
本来这种例子想用XX文库的阅读方式解决。
为实现见效块的方针,暂用此功能代替,后期再研发XX文库这种在线阅读器。
2.根据现几个客户的需求,需要对红文格式进行统一成ceb格式,又为了能够让用户能再流程中进行在线编辑,我们可以结合word和ceb双重形式,一再流程运转过程中我们保留word在线编辑,在制作红文的同时,我们帮他同时多生成一个ceb文件,然后根据政府机关的条件,决定是用方正的ceb盖章功能,还是用我们的电子印章。
最后发送到我们的公文交换系统全部采用ceb格式。
对于jacob操作word还有很多功能,我从网上下载了一份jacob操作word的api实例,大家可以看看。
如下:
importcom.jacob.activeX.ActiveXComponent;
import.Dispatch;
import.Variant;
/**
*jacob操作MSword类
*
*@author
*/
publicclassWordBean{
//word文档
privateDispatchdoc;
//word运行程序对象
privateActiveXComponentword;
//所有word文档集合
privateDispatchdocuments;
//选定的范围或插入点
privateDispatchselection;
privatebooleansaveOnExit=true;
publicWordBean(){
if(word==null){
word=newActiveXComponent("Word.Application");
word.setProperty("Visible",newVariant(false));//不可见打开word
word.setProperty("AutomationSecurity",newVariant(3));//禁用宏
}
if(documents==null){
try{
documents=word.getProperty("Documents").toDispatch();
}catch(Exceptione){
e.printStackTrace();
}
}
}
/**
*设置退出时参数
*
*@paramsaveOnExit
*booleantrue-退出时保存文件,false-退出时不保存文件
*/
publicvoidsetSaveOnExit(booleansaveOnExit){
this.saveOnExit=saveOnExit;
}
/**
*创建一个新的word文档
*
*/
publicvoidcreateNewDocument(){
doc=Dispatch.call(documents,"Add").toDispatch();
selection=Dispatch.get(word,"Selection").toDispatch();
}
/**
*打开一个已存在的文档
*
*@paramdocPath
*/
publicvoidopenDocument(StringdocPath){
closeDocument();
doc=Dispatch.call(documents,"Open",docPath).toDispatch();
selection=Dispatch.get(word,"Selection").toDispatch();
}
/**
*打开一个保护文档,
*
*@paramdocPath
*-文件全名
*@parampwd
*-密码
*/
publicvoidopenDocumentOnlyRead(StringdocPath,Stringpwd)
throwsException{
closeDocument();
//doc=Dispatch.invoke(documents,"Open",Dispatch.Method,
//newObject[]{docPath,newVariant(false),newVariant(true),new
//Variant(true),pwd},
//newint[1]).toDispatch();//打开word文件
doc=Dispatch.callN(
documents,
"Open",
newObject[]{docPath,newVariant(false),newVariant(true),
newVariant(true),pwd,"",newVariant(false)})
.toDispatch();
selection=Dispatch.get(word,"Selection").toDispatch();
}
publicvoidopenDocument(StringdocPath,Stringpwd)throwsException{
closeDocument();
doc=Dispatch.callN(
documents,
"Open",
newObject[]{docPath,newVariant(false),newVariant(false),
newVariant(true),pwd}).toDispatch();
selection=Dispatch.get(word,"Selection").toDispatch();
}
/**
*把选定的内容或插入点向上移动
*
*@parampos
*移动的距离
*/
publicvoidmoveUp(intpos){
if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();
for(inti=0;iDispatch.call(selection,"MoveUp");
}
/**
*把选定的内容或者插入点向下移动
*
*@parampos
*移动的距离
*/
publicvoidmoveDown(intpos){
if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();
for(inti=0;iDispatch.call(selection,"MoveDown");
}
/**
*把选定的内容或者插入点向左移动
*
*@parampos
*移动的距离
*/
publicvoidmoveLeft(intpos){
if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();
for(inti=0;iDispatch.call(selection,"MoveLeft");
}
}
/**
*把选定的内容或者插入点向右移动
*
*@parampos
*移动的距离
*/
publicvoidmoveRight(intpos){
if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();
for(inti=0;iDispatch.call(selection,"MoveRight");
}
/**
*把插入点移动到文件首位置
*
*/
publicvoidmoveStart(){
if(selection==null)
selection=Dispatch.get(word,"Selection").toDispatch();
Dispatch.call(selection,"HomeKey",newVariant(6));
}
/**
*从选定内容或插入点开始查找文本
*
*@paramtoFindText
*要查找的文本
*@returnbooleantrue-查找到并选中该文本,false-未查找到文本
*/
@SuppressWarnings("static-access")
publicbooleanfind(StringtoFindText){
if(toFindText==null||toFindText.equals(""))
returnfalse;
//从selection所在位置开始查询
Dispatchfind=word.call(selection,"Find").toDispatch();
//设置要查找的内容
Dispatch.put(find,"Text",toFindText);
//向前查找
Dispatch.put(find,"Forward","True");
//设置格式
Dispatch.put(find,"Format","True");
//大小写匹配
Dispatch.put(find,"MatchCase","True");
//全字匹配
Dispatch.put(find,"MatchWholeWord","True");
//查找并选中
returnDispatch.call(find,"Execute").getBoolean();
}
/**
*把选定选定内容设定为替换文本
*
*@paramtoFindText
*查找字符串
*@paramnewText
*要替换的内容
*@return
*/
publicbooleanreplaceText(StringtoFindText,StringnewText){
if(!
find(toFindText))
returnfalse;
Dispatch.put(selection,"Text",newText);
returntrue;
}
/**
*全局替换文本
*
*@paramtoFindText
*查找字符串
*@paramnewText
*要替换的内容
*/
publicvoidreplaceAllText(StringtoFindText,StringnewText){
while(find(toFindText)){
Dispatch.put(selection,"Text",newText);
Dispatch.call(selection,"MoveRight");
}
}
/**
*在当前插入点插入字符串
*
*@paramnewText
*要插入的新字符串
*/
publicvoidinsertText(StringnewText){
Dispatch.put(selection,"Text",newText);
}
/**
*
*@paramtoFindText
*要查找的字符串
*@paramimagePath
*图片路径
*@return
*/
publicbooleanreplaceImage(StringtoFindText,StringimagePath){
if(!
find(toFindText))
returnfalse;
Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),
"AddPicture",imagePath);
returntrue;
}
/**
*全局替换图片
*
*@paramtoFindText
*查找字符串
*@paramimagePath
*图片路径
*/
publicvoidreplaceAllImage(StringtoFindText,StringimagePath){
while(find(toFindText)){
Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),
"AddPicture",imagePath);
Dispatch.call(selection,"MoveRight");
}
}
/**
*在当前插入点插入图片
*
*@paramimagePath
*图片路径
*/
publicvoidinsertImage(StringimagePath){
Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),
"AddPicture",imagePath);
}
/**
*合并单元格
*
*@paramtableIndex
*@paramfstCellRowIdx
*@paramfstCellColIdx
*@paramsecCellRowIdx
*@paramsecCellColIdx
*/
publicvoidmergeCell(inttableIndex,intfstCellRowIdx,intfstCellColIdx,