Java代碼在Word中的指定位置插入一張圖片

Java代碼在Word中的指定位置插入一張圖片

在繼使用java向word中插入文字,使用java代碼將word轉爲pdf之後,在寫一篇使用java代碼將圖片插入到word文檔中。
噢~我無所不能的java語言。
開心
我們插入下面這張圖片,這是原圖,挺大的。
在這裏插入圖片描述
話不多說上代碼。

import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordUtil_img {
	
	// word運行程序對象
	private ActiveXComponent word;
	// 所有word文檔集合
	private Dispatch documents;
	// word文檔
	private Dispatch doc;
	// 選定的範圍或插入點
	private Dispatch selection;
	// 保存退出
	private boolean saveOnExit;
	
	/**
	 * 是否可見word程序
	 * @param visible true-可見word程序,false-後臺默默執行。
	 */
	public WordUtil_img(boolean visible) {
		word = new ActiveXComponent("Word.Application");
		word.setProperty("Visible", new Variant(visible));
		documents = word.getProperty("Documents").toDispatch();
	}
	/**
	 * 打開一個已經存在的Word文檔
	 * @param docPath 文件的路徑
	 */
	public void openDocument(String docPath) {
		doc = Dispatch.call(documents, "Open", docPath).toDispatch();
		selection = Dispatch.get(word, "Selection").toDispatch();
	}

	/**
	 * 全局將指定的文本替換成圖片
	 * @param findText
	 * @param imagePath
	 */
	public void replaceAllImage(String findText, String imagePath, int width, int height){
		moveStart();
		while (find(findText)){
			Dispatch picture = Dispatch.call(Dispatch.get(getSelection(), "InLineShapes").toDispatch(), "AddPicture", imagePath).toDispatch();
			Dispatch.call(picture, "Select");
			Dispatch.put(picture, "Width", new Variant(width));
			Dispatch.put(picture, "Height", new Variant(height));
			moveStart();
		}
	}
	
	/**
	 * 把插入點移動到文件首位置
	 */
	public void moveStart(){
		Dispatch.call(getSelection(), "HomeKey", new Variant(6));
	}
	
	/**
	 * 獲取當前的選定的內容或者插入點
	 * @return 當前的選定的內容或者插入點
	 */
	public Dispatch getSelection(){
		if (selection == null)
			selection = Dispatch.get(word, "Selection").toDispatch();
		return selection;
	}
	
	/**
	 * 從選定內容或插入點開始查找文本
	 * @param findText 要查找的文本
	 * @return boolean true-查找到並選中該文本,false-未查找到文本
	 */
	public boolean find(String findText){
		if(findText == null || findText.equals("")){
			return false;
		}
		// 從selection所在位置開始查詢
		Dispatch find = Dispatch.call(getSelection(), "Find").toDispatch();
		// 設置要查找的內容
		Dispatch.put(find, "Text", findText);
		// 向前查找
		Dispatch.put(find, "Forward", "True");
		// 設置格式
		Dispatch.put(find, "Format", "True");
		// 大小寫匹配
		Dispatch.put(find, "MatchCase", "True");
		// 全字匹配
		Dispatch.put(find, "MatchWholeWord", "True");
		// 查找並選中
		return Dispatch.call(find, "Execute").getBoolean();
	}
	
	/**
	 * 文檔另存爲
	 * @param savePath
	 */
	public void saveAs(String savePath){
		Dispatch.call(doc, "SaveAs", savePath);
	}
	
	/**
	 * 關閉word文檔
	 */
	public void closeDocument(){
		if (doc != null) {
			Dispatch.call(doc, "Close", new Variant(saveOnExit));
			doc = null;
		}
	}
}

以上就是工具類的源碼了,下面我們可以寫一個main函數,測試一下。主要調用的是工具類中 replaceAllImage()這個方法。

public static void main(String[] args) {
		WordInsertImg("F:\\我是word.doc","F:\\pikaqiu,jpg");
	}
	
	public static void WordInsertImg(String WordPath,String ImgPaht) {
		WordUtil_img demo = new WordUtil_img(false);//獲取工具類對象
		demo.openDocument(WordPath);//打開word
		// 在指定位置插入指定的圖片
		demo.replaceAllImage("AAA",ImgPaht,60,60);//AAA是指定的圖片位置。後面的兩個參數是指定圖片的長和寬。
		demo.saveAs("f:\\我是皮卡丘.doc");//插入成功後生成的新word
		demo.closeDocument();//關閉對象。
		System.out.println("插入成功");
	}

在F盤下有 "我是word.doc"這個文件。以及名稱爲 pikaqiu.jpg 圖片。

tup
word中有“AAA”這個字段,準備替換。
word中有“AAA”這個字段,準備替換。
下面我們運行程序。
顯示插入成功
顯示插入圖片成功,下面我們去F盤看一下吧。
在這裏插入圖片描述
可以看到已經有生成的新word打開以後是什麼?
在這裏插入圖片描述
ok,可以看到我們的圖片已經插入進來了。而且圖片也沒有那麼大,被固定成60*60的大小。
這個jar和插入文字的是一樣的、所以沒有多餘的jar包。可以去我的另一篇文章 將數據寫入word文檔中下載jar包

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章