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:菜雞寫博客,有問題請指正!!!