代码如下:
1.Application类
package com.ibm.ahcms2.tool.jacob.word;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* Word应用程序类
* 说明:
* 作者:何杨(heyang78@gmail.com)
* 创建时间:2011-6-4 下午05:16:47
* 修改时间:2011-6-4 下午05:16:47
*/
public class Application{
// Word应用程序本身
private ActiveXComponent wordApp;
// Word的文档集合对象
private Documents documents;
/**
* 构造函数
*/
public Application() throws Exception{
initialize();
}
/**
* 应用程序初始化
*
* 说明:
* 创建时间:2011-6-4 下午05:17:59
*/
public void initialize() throws Exception{
// 初始化com的线程,使用结束后要调用realease方法,见quit函数
ComThread.InitSTA();
wordApp=new ActiveXComponent("Word.Application");
wordApp.setProperty("Visible", new Variant(false));
Dispatch d=wordApp.getProperty("Documents").toDispatch();
documents=new Documents(d);
}
/**
* 应用程序退出
*
* 说明:
* 创建时间:2011-6-4 下午05:18:56
*/
public void quit() throws Exception{
wordApp.invoke("Quit", new Variant[]{});
ComThread.Release();
}
/**
* 新建文档,并返回新建文档的句柄
*
* 说明:
* @return
* @throws Exception
* 创建时间:2011-6-4 下午05:33:07
*/
public Document addNewDocument() throws Exception{
Dispatch d=Dispatch.call(documents.getInstance(),"Add").toDispatch();
Document doc=new Document(d);
return doc;
}
/**
* 得到当前选择的文字
*
* 说明:
* @return
* @throws Exception
* 创建时间:2011-6-4 下午05:38:28
*/
public Selection getSelection() throws Exception{
Dispatch d=Dispatch.call(wordApp,"Selection").toDispatch();
Selection selection=new Selection(d,wordApp);
return selection;
}
/**
* 打开一个已存在的文档
*
* 说明:
* @param filePathName
* @return
* @throws Exception
* 创建时间:2011-6-4 下午06:19:41
*/
public Document openExistDocument(String filePathName) throws Exception{
Dispatch d = Dispatch.call(documents.getInstance(), "Open", filePathName).toDispatch();
Document doc=new Document(d);
return doc;
}
}
2.BaseWord类
package com.ibm.ahcms2.tool.jacob.word;
import com.jacob.com.Dispatch;
public abstract class BaseWord{
protected Dispatch instance;
public BaseWord(Dispatch instance){
this.instance=instance;
}
public Dispatch getInstance() {
return instance;
}
public void setInstance(Dispatch instance) {
this.instance = instance;
}
}
3.Document类
package com.ibm.ahcms2.tool.jacob.word;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 文档对象
* 说明:
* 作者:何杨(heyang78@gmail.com)
* 创建时间:2011-6-4 下午05:41:47
* 修改时间:2011-6-4 下午05:41:47
*/
public class Document extends BaseWord{
public Document(Dispatch instance) {
super(instance);
}
/**
* 文档另存为
*
* 说明:
* @param filePathName
* @throws Exception
* 创建时间:2011-6-4 下午05:42:42
*/
public void saveAs(String filePathName) throws Exception{
Dispatch.call(instance, "SaveAs", filePathName);
}
public void save() throws Exception{
Dispatch.call(instance, "Save");
}
/**
* 关闭文档
*
* 说明:
* @throws Exception
* 创建时间:2011-6-4 下午05:43:52
*/
public void close() throws Exception{
Dispatch.call(instance, "Close", new Variant(true));
}
/**
* 设置页眉的文字
*
* 说明:
* @param headerText
* @throws Exception
* 创建时间:2011-6-4 下午07:22:37
*/
public void setHeaderText(String headerText,Dispatch selection) throws Exception{
Dispatch activeWindow = Dispatch.get(instance, "ActiveWindow").toDispatch();
Dispatch view = Dispatch.get(activeWindow, "View").toDispatch();
Dispatch.put(view, "SeekView", new Variant(9)); //wdSeekCurrentPageHeader-9
Dispatch headerFooter = Dispatch.get(selection, "HeaderFooter").toDispatch();
Dispatch range = Dispatch.get(headerFooter, "Range").toDispatch();
Dispatch.put(range, "Text", new Variant(headerText));
Dispatch font = Dispatch.get(range, "Font").toDispatch();
Dispatch.put(font, "Name", new Variant("楷体_GB2312"));
Dispatch.put(font, "Bold", new Variant(true));
Dispatch.put(font, "Size", 9);
Dispatch.put(view, "SeekView", new Variant(0)); //wdSeekMainDocument-0恢复视图;
}
/**
* 设置图片水印
*
* 说明:
* @param imagePath
* @param selection
* @throws Exception
* 创建时间:2011-6-4 下午07:48:53
*/
public void setImageWaterMark(String imagePath,Dispatch selection) throws Exception{
Dispatch activeWindow = Dispatch.get(instance, "ActiveWindow").toDispatch();
Dispatch view = Dispatch.get(activeWindow, "View").toDispatch();
Dispatch.put(view, "SeekView", new Variant(9)); //wdSeekCurrentPageHeader-9
Dispatch headerFooter = Dispatch.get(selection, "HeaderFooter").toDispatch();
// 获取水印图形对象
Dispatch shapes=Dispatch.get(headerFooter, "Shapes").toDispatch();
Dispatch picture=Dispatch.call(shapes, "AddPicture",imagePath).toDispatch();
Dispatch.call(picture, "Select");
Dispatch.put(picture,"Left",new Variant(10));
Dispatch.put(picture,"Top",new Variant(10));
Dispatch.put(picture,"Width",new Variant(190));
Dispatch.put(picture,"Height",new Variant(190));
Dispatch.put(view, "SeekView", new Variant(0)); //wdSeekMainDocument-0恢复视图;
}
/**
* 设置图片水印
*
* 说明:
* @param imagePath
* @param selection
* @param left
* @param top
* @param width
* @param height
* @throws Exception
* 创建时间:2011-6-4 下午08:00:16
*/
public void setImageWaterMark(String imagePath,Dispatch selection,int left,int top,int width,int height) throws Exception{
Dispatch activeWindow = Dispatch.get(instance, "ActiveWindow").toDispatch();
Dispatch view = Dispatch.get(activeWindow, "View").toDispatch();
Dispatch.put(view, "SeekView", new Variant(9)); //wdSeekCurrentPageHeader-9
Dispatch headerFooter = Dispatch.get(selection, "HeaderFooter").toDispatch();
// 获取水印图形对象
Dispatch shapes=Dispatch.get(headerFooter, "Shapes").toDispatch();
Dispatch picture=Dispatch.call(shapes, "AddPicture",imagePath).toDispatch();
Dispatch.call(picture, "Select");
Dispatch.put(picture,"Left",new Variant(left));
Dispatch.put(picture,"Top",new Variant(top));
Dispatch.put(picture,"Width",new Variant(width));
Dispatch.put(picture,"Height",new Variant(height));
Dispatch.put(view, "SeekView", new Variant(0)); //wdSeekMainDocument-0恢复视图;
}
/**
* 给文档加上保护
*
* 说明:
* @param pswd
* @throws Exception
* 创建时间:2011-6-4 下午07:33:44
*/
public void setProtected(String pswd) throws Exception{
String protectionType = Dispatch.get(instance, "ProtectionType").toString();
if(protectionType.equals("-1")){
Dispatch.call(instance, "Protect", new Variant(3), new Variant(true), pswd);
}
}
/**
* 给文档解除保护
*
* 说明:
* @param pswd
* @throws Exception
* 创建时间:2011-6-4 下午07:38:04
*/
public void releaseProtect(String pswd) throws Exception{
String protectionType = Dispatch.get(instance, "ProtectionType").toString();
if(protectionType.equals("3")){
Dispatch.call(instance, "Unprotect", pswd);
}
}
}
4.Documents类
package com.ibm.ahcms2.tool.jacob.word;
import com.jacob.com.Dispatch;
public class Documents extends BaseWord{
public Documents(Dispatch instance) {
super(instance);
}
}
5.Selection类
package com.ibm.ahcms2.tool.jacob.word;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
/**
* 一个文档中选择的部分
* 说明:
* 作者:何杨(heyang78@gmail.com)
* 创建时间:2011-6-4 下午05:37:01
* 修改时间:2011-6-4 下午05:37:01
*/
public class Selection extends BaseWord{
private ActiveXComponent wordApp;
public Selection(Dispatch instance,ActiveXComponent wordApp) {
super(instance);
this.wordApp=wordApp;
}
/**
* 设置选择区的文字
*
* 说明:
* @param text
* @throws Exception
* 创建时间:2011-6-4 下午05:41:28
*/
public void setText(String text) throws Exception{
Dispatch.put(instance, "Text", text);
}
/**
* 用新文字替换旧文字
*
* 说明:
* @param oldText
* @param newText
* @throws Exception
* 创建时间:2011-6-4 下午06:32:43
*/
public void replace(String oldText,String newText) throws Exception{
while (find(oldText)) {
Dispatch.put(instance, "Text", newText);
Dispatch.call(instance, "MoveRight");
}
}
/**
* 查找字符串
*
* 说明:
* @param toFindText
* @return
* 创建时间:2011-6-4 下午07:15:39
*/
private boolean find(String toFindText) {
if (toFindText == null || toFindText.equals(""))
return false;
// 从selection所在位置开始查询
Dispatch find = wordApp.call(instance, "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", "False");
// 查找并选中
return Dispatch.call(find, "Execute").getBoolean();
}
/**
* 插入图片
*
* 说明:
* @param imagePath
* 创建时间:2011-6-4 下午07:17:18
*/
public void insertPicture(String imagePath){
Dispatch.call(Dispatch.get(instance, "InLineShapes").toDispatch(),
"AddPicture", imagePath);
}
}
6.测试代码
package com.ibm.ahcms2.tool.jacob.word;
public class Test{
public static void main(String[] args) throws Exception{
test09();
}
// 设置页眉,图片水印以及设置保护
public static void test09() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\2.doc");
Selection sel=app.getSelection();
doc.setHeaderText("页眉文字", sel.getInstance());
doc.setImageWaterMark("c:\\1.jpg", sel.getInstance(),20,20,200,200);
doc.setProtected("cms123");
doc.save();
doc.close();
app.quit();
}
// 设置图片水印
public static void test08() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\1.doc");
Selection sel=app.getSelection();
doc.setImageWaterMark("c:\\1.jpg", sel.getInstance());
doc.save();
doc.close();
app.quit();
}
// 解除密码保护
public static void test07() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\1.doc");
doc.releaseProtect("cms123");
doc.save();
doc.close();
app.quit();
}
// 设置密码保护
public static void test06() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\1.doc");
doc.setProtected("cms123");
doc.save();
doc.close();
app.quit();
}
// 设置表头
public static void test05() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\1.doc");
Selection sel=app.getSelection();
doc.setHeaderText("妞妞乖乖", sel.getInstance());
doc.save();
doc.close();
app.quit();
}
// 插入图片
public static void test04() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\1.doc");
Selection sel=app.getSelection();
sel.insertPicture("c:\\1.jpg");
doc.save();
doc.close();
app.quit();
}
// 打开文档,用新文字替换旧文字后保存
public static void test03() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\1.doc");
Selection sel=app.getSelection();
sel.replace("Success", "胜利");
doc.save();
doc.close();
app.quit();
}
// 打开文档,重新设置文字并保存
public static void test02() throws Exception{
Application app=new Application();
Document doc=app.openExistDocument("c:\\1.doc");
Selection sel=app.getSelection();
sel.setText("又胜利了!何杨,2011年6月4日18:23:46");
doc.save();
doc.close();
app.quit();
}
// 新建文档,设置文档并另存为
public static void test01() throws Exception{
Application app=new Application();
Document doc=app.addNewDocument();
Selection sel=app.getSelection();
sel.setText("胜利了");
doc.saveAs("c:\\1.doc");
doc.close();
app.quit();
}
}
以上代码下载:
http://www.blogjava.net/Files/heyang/JacobTest20110604201829.rartest09效果如下: