一、效果图对比(文本"d"和图片是后台动态加的)
之前:
之后:
二、首先在需要插入文本或图片位置设置书签
三、通过书签替换后台代码如下:
String doc_path = "E:\\apache-tomcat-8.0.30\\webapps\\doc\\aa.doc";//word文档所在路径
String img_path = "E:\\apache-tomcat-8.0.30\\webapps\\doc\\img\\aa.png";//图片所在路径
insertPicByjacob(doc_path, doc_path, "PO_user_name1", img_path);
/**
* 使用jacob动态替换word书签
* @param templatePath 原word路径
* @param targetPath 另存为word路径
* @param bookmark 书签名
* @param imagePath 要插入图片的路径
*/
public void insertPicByjacob(String templatePath, String targetPath, String bookmark, String imagePath) {
System.out.println("启动word...");
ActiveXComponent app = null;
Dispatch doc = null;
// 模板的路径
String openPath = templatePath;
// 要保存的文件的路径
String toFileName = targetPath;
Dispatch docs = null;
if (app == null || app.m_pDispatch == 0) {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
app.setProperty("DisplayAlerts", new Variant(false));
}
if (docs == null) {
// 获得documents对象
docs = app.getProperty("Documents").toDispatch();
}
doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { openPath, new Variant(false), new Variant(true) }, new int[1])
.toDispatch();
System.out.println("打开文档..." + openPath);
Dispatch book_marks = app.call(doc, "Bookmarks").toDispatch();
int bCount = Dispatch.get(book_marks, "Count").getInt(); //获取书签数
//将书签列表存放到list + map 结构中
String userName = SessionUser.getUserName();
for (int i = 1; i <= bCount; i++){
Dispatch items = Dispatch.call(book_marks, "Item", i).toDispatch();
String bookMarkKey = String.valueOf(Dispatch.get(items, "Name").getString()).replaceAll("null", ""); //读取书签命名
Dispatch range = Dispatch.get(items, "Range").toDispatch();
String bookMarkValue = String.valueOf(Dispatch.get(range, "Text").getString()).replaceAll("null", ""); //读取书签文本
String MarkKey = bookMarkKey;
String MarkValue = bookMarkValue;
//书签名并替换的内容
if( MarkKey.equals(bookmark))//书签名为PO_user_name1
{
MarkValue = userName; //该PO_user_name1书签处插入登录人名字;
Dispatch.put(range, "Text", new Variant(MarkValue));//替换文字
//替换图片
Dispatch picture = Dispatch.call(Dispatch.get(range, "InLineShapes").toDispatch(), "AddPicture", imagePath).toDispatch();
Dispatch.call(picture, "Select"); // 选中图片
Dispatch.put(picture, "Width", new Variant(100)); // 图片的宽度
Dispatch.put(picture, "Height", new Variant(100)); // 图片的高度
Dispatch ShapeRange = Dispatch.call(picture, "ConvertToShape").toDispatch(); // 取得图片区域
Dispatch WrapFormat = Dispatch.get(ShapeRange, "WrapFormat").toDispatch(); // 取得图片的格式对象
Dispatch.put(WrapFormat, "Type", 5); // 图片的环绕类型
}
}
// 保存文件//new variant() 参数 0Doc 12、16Docx 17pdf
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { targetPath, new Variant(12) }, new int[1]);
Dispatch.call((Dispatch) doc, "Close", new Variant(false));
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
注:Dispatch.put(WrapFormat, "Type", 5) 图片的环绕类型如下
设置环绕格式(0 - 7)下面是参数说明
wdWrapSquare 0 使文字环绕形状。行在形状的另一侧延续。
wdWrapTight 1 使文字紧密地环绕形状。
wdWrapThrough 2 使文字环绕形状。
wdWrapNone 3 将形状放在文字前面。请参阅 wdWrapFront 。
wdWrapTopBottom 4 将文字放在形状的上方和下方。
wdWrapBehind 5 将形状放在文字上面。
wdWrapFront 6 将形状放在文字下面。
wdWrapInline 7 将形状嵌入到文字中。
四、若想通过全文查找方式替换代码如下:
/**
* 使用jacob动态替换word书签
* @param templatePath 原word路径
* @param targetPath 另存为word路径
* @param bookmark 要替换的内容名
* @param imagePath 要插入图片的路径
*/
public void insertPicByjacob(String templatePath, String targetPath, String word, String imagePath) {
System.out.println("启动word...");
ActiveXComponent app = null;
Dispatch doc = null;
// 模板的路径
String openPath = templatePath;
// 要保存的文件的路径
String toFileName = targetPath;
Dispatch docs = null;
if (app == null || app.m_pDispatch == 0) {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
app.setProperty("DisplayAlerts", new Variant(false));
}
if (docs == null) {
// 获得documents对象
docs = app.getProperty("Documents").toDispatch();
}
doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { openPath, new Variant(false), new Variant(true) }, new int[1])
.toDispatch();
System.out.println("打开文档..." + openPath);
Dispatch selection = app.getProperty("Selection").toDispatch();
Dispatch find = Dispatch.call(selection, "Find").toDispatch();// 获得Find组件
Dispatch.put(find, "Text", word); // 查找字符串
Dispatch.put(find, "MatchWholeWord", "True"); // 全字匹配
boolean bl = Dispatch.call(find, "Execute").getBoolean(); // 执行查询
Dispatch activeDocument = app.getProperty("ActiveDocument")
.toDispatch();
Dispatch bookMarks = app.call(activeDocument, "Bookmarks")
.toDispatch();
boolean bookMarkExist = app.call(bookMarks, "Exists", word)
.toBoolean();
if (bookMarkExist) {
Dispatch picture = Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath).toDispatch();
Dispatch.call(picture, "Select"); // 选中图片
Dispatch.put(picture, "Width", new Variant(100)); // 图片的宽度
Dispatch.put(picture, "Height", new Variant(100)); // 图片的高度
Dispatch ShapeRange = Dispatch.call(picture, "ConvertToShape").toDispatch(); // 取得图片区域
Dispatch WrapFormat = Dispatch.get(ShapeRange, "WrapFormat").toDispatch(); // 取得图片的格式对象
Dispatch.put(WrapFormat, "Type", 5);//图片的环绕类型
System.out.println(bookMarkExist);
}
// 保存文件//new variant() 参数 0Doc 12、16Docx 17pdf
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { targetPath, new Variant(12) }, new int[1]);
Dispatch.call((Dispatch) doc, "Close", new Variant(false));
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
五、 jacob压缩包
解压后把jacob-1.18-x64.dll文件放到jre\bin的目录下
这里我只放到jre\bin下并重新添加jdk后没生效,又添加到了项目所在tomcat\bin的目录下就可以了