jacob 文件類型轉換

jacob簡介

 

jacob操作文檔轉換底層還是調用windows的 office去轉換,office是建立在windows平臺之上的,本身是一個軟件,除了他自己提供的宏似乎沒有什麼能對他進行直接的操作,在windows平臺上爲了解決像這樣的不同應用軟件,通信缺乏通用api問題,推出了com的解決方案。我們使用dll中的一組或多組相關的函數存取組件數據,總的合稱爲接口(對應jacob,就是Dispatch),具體到每個細節的實現稱爲方法。我們使用jacob就是通過一個接口來操作word的ActiveX對象(實質是調用指向接口的指針,這也是唯一途徑)。

所以在使用jacob進行文檔轉換時需要用到jacob-1.19-x64.dll;jacob-1.19-x86.dll去做底層的操作。

核心類介紹

  • JacobObject:用於Java程序MS下的COM進行通信,創建標準的API框架
  • ComThread:初始化COM組件線程,釋放線程,對線程進行管理
  • Dispatch:調度處理類,封裝了操作來從而操作Office,並表示不同MS級別調度對象
  • ActiveXComponent : 創建COM組件
  • Variant : 與COM通訊的參數或者返回值
  • ROT :Running Object Table (ROT),運行對象表將每個線程映射到所有jacobobjects,在線程創建核心方法

核心方法介紹

  • Dispatch : 可調用該自動化對象的屬性或方法,具體的屬性和方法要看參考文檔VBA API
  • Dispatch.get(dispatch, String name);獲取對象屬性
  • Dispatch.put(dispatch, String name, Object value);設置對象屬性
  • Dispatch.call(dispatch, String name, Object… args);調用對象方法

類圖介紹

類圖
類圖

使用流程

流程

使用步驟

  1. 下載jar包https://pan.baidu.com/s/1cilYcypa8Vt-5-uNIqXDIg  提取碼:16fl
  2. 下載dll文件,放在jdk 的bin目錄下。(https://pan.baidu.com/s/1PumFrMC9fahEzmvdnz8gRQ   提取碼:zsh2)
  3. 本機電腦中要有 office全家桶 word、xls,ppt 什麼的.....
  4. 注意jdk版本什麼的,以免出問題。

試例代碼

//word轉pdf
	public static void doc2PDF(String srcFilePath, String pdfFilePath) throws Exception {
		ActiveXComponent app = null;
		Dispatch doc = null;
		try {
			//初始化COM組件線程
			ComThread.InitSTA();
			//創建com組件,打開word應用程序
			app = new ActiveXComponent("Word.Application");
			//設置word不可見
			app.setProperty("Visible", new Variant(false));
			//獲得word中所有打開的文檔,返回Documents對象
			Dispatch docs = app.getProperty("Documents").toDispatch();
			Object[] obj = new Object[] { srcFilePath, new Variant(false), new Variant(false), // 是否只讀
					new Variant(false), new Variant("pwd") };
			doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();//
			//Dispatch.put(doc, "Compatibility", false);  //兼容性檢查,爲特定值false不正確  
			Dispatch.put(doc, "RemovePersonalInformation", false);
			// word保存爲pdf格式宏,值爲17
			Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); 
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (doc != null) {
				Dispatch.call(doc, "Close", false);
			}
			if (app != null) {
				app.invoke("Quit", new Variant[] {});
			}
			//結束後關閉線程組件
			ComThread.Release();
		}
	}
//ppt轉pdf
	public static void ppt2PDF(String srcFilePath, String pdfFilePath) throws Exception {
		ActiveXComponent app = null;
		Dispatch ppt = null;
		try {
			ComThread.InitSTA();
			app = new ActiveXComponent("PowerPoint.Application");
			Dispatch ppts = app.getProperty("Presentations").toDispatch();
			/*
			 * call param 4: ReadOnly param 5: Untitled指定文件是否有標題 param 6: WithWindow指定文件是否可見
			 */
			ppt = Dispatch.call(ppts, "Open", srcFilePath, true, true, false).toDispatch();
			Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF爲特定值32
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (ppt != null) {
				Dispatch.call(ppt, "Close");
			}
			if (app != null) {
				app.invoke("Quit", new Variant[] {});
			}
			ComThread.Release();
		}
	}
//excel轉pdf
	public static void excel2PDF(String inFilePath, String outFilePath) throws Exception {
		ActiveXComponent ax = null;
		Dispatch excel = null;
		try {
			ComThread.InitSTA();
			ax = new ActiveXComponent("Excel.Application");
			ax.setProperty("Visible", new Variant(false));
			ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
			Dispatch excels = ax.getProperty("Workbooks").toDispatch();
			Object[] obj = new Object[] { inFilePath, new Variant(false), new Variant(false) };
			excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();
			// 轉換格式
			Object[] obj2 = new Object[] { new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0
					outFilePath, new Variant(0) // 0=標準 (生成的PDF圖片不會變模糊) ; 1=最小文件
			};
			Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (excel != null) {
				Dispatch.call(excel, "Close", new Variant(false));
			}
			if (ax != null) {
				ax.invoke("Quit", new Variant[] {});
				ax = null;
			}
			ComThread.Release();
		}
	}

注:路徑問題

inFilePath:當前文檔所在位置;outFilePath:轉pdf後的文件保存位置

如部署在tomcat的某個文件下:

ServletContext servletContext = getRequest().getSession().getServletContext();
String realPath = servletContext.getRealPath("/");
String inFilePath = realPath + "/data/supp/file_upload/PRJFILE/"+recordId;
String outFilePath = realPath +"/data/supp/file_upload/PRJFILE/pdf/"+recordId+".pdf";

 

 

 

 

 

 

 

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