1、操作word需要的jacobjar包和dll文件。(请多多支持!)
java使用jacob给word添加水印。这是给word添加水印,当业务需求在多页word文档中,某一页添加水印一样的盖章,这个链接就没有办法了。
2、在word中需要盖章的位置添加个书签。
3、用jacob给书签替换成图片
/**
* Created by qiuzhb on 2018/6/11.
*/
public class Test0611 {
private Dispatch activeWindow = null;
public static void main(String []args) {
Test0611 test0611 = new Test0611();
test0611.insertPicByjacob("D:\\Users\\qiuzhb\\Downloads\\20180529113238\\SW_20180529113240_2.doc",
"D:\\Users\\qiuzhb\\Downloads\\20180529113238\\SW_20180529113240_3.doc",
"picture","C:\\gajyz.GIF");
}
public void insertImageAtBookmark(String bookmarkName, String imagePath, int width, int height){
//Dispatch dispatch = getBookmark(bookmarkName);
Dispatch dispatch = Dispatch.get(this.activeWindow, "ActivePane").toDispatch();
if(dispatch != null){
Dispatch picture = Dispatch.call(Dispatch.get(dispatch, "InLineShapes").toDispatch(), "AddPicture", imagePath).toDispatch();
Dispatch.call(picture, "Select");
Dispatch.put(picture, "Width", new Variant(width));
Dispatch.put(picture, "Height", new Variant(height));
Dispatch ShapeRange = Dispatch.call(picture, "ConvertToShape").toDispatch(); // 取得图片区域
Dispatch WrapFormat = Dispatch.get(ShapeRange, "WrapFormat").toDispatch(); // 取得图片的格式对象
Dispatch.put(WrapFormat, "Type", 5); // 设置环绕格式(0 - 7)下面是参数说明
// wdWrapInline 7 将形状嵌入到文字中。
// wdWrapNone 3 将形状放在文字前面。请参阅 wdWrapFront 。
// wdWrapSquare 0 使文字环绕形状。行在形状的另一侧延续。
// wdWrapThrough 2 使文字环绕形状。
// wdWrapTight 1 使文字紧密地环绕形状。
// wdWrapTopBottom 4 将文字放在形状的上方和下方。
// wdWrapBehind 5 将形状放在文字后面。
// wdWrapFront 6 将形状放在文字前面。
Dispatch.call(picture, "Select");
Dispatch.put(picture, "Left", new Variant(350));
Dispatch.put(picture, "Top", new Variant(350));
}
}
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[] {});
}
}
4、如果你和我一样,是先利用ftl模板创建word,然后再插入图片,记得创建word后让主线程睡一会,不然创建完word立马插入图片会报错,好像是写入word的io还没有结束。
ps:菜鸡写博客,有问题请指正!!!