Home  >  Article  >  Web Front-end  >  使用jacob调用Windows的com对象,转换Office文件为pdf、html等_html/css_WEB-ITnose

使用jacob调用Windows的com对象,转换Office文件为pdf、html等_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:50:091144browse

1、介绍


     Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。至于什么是COM组件,大家自己Google吧。

2、安装和配置

    Jacob是一个开源软件,它的官方站点是: http://danadler.com/jacob/ 大家可以到上面下载源代码研究,也可以直接下载编译后的二进制文件。

    下载包jacob_x.x.zip,解压后有几个文件:jacob.jar、jacob-x.x-M2-x86.dll
    把jacob-x.x-M2-x86.dll拷贝到%JAVA_HOME% 下的 bin 目录下,其中,%JAVA_HOME%就是JDK的安装目录。接着直接在java IDE中引用jacob.jar就可以使用了。
     

3、转换word为pdf、html、txt 的示例

package com.shanhy.demo.windowsoffice;import java.io.File;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;/** * * 将jacob.dll放入JDK的bin目录下 * 把jacob.jar放入项目的buildPath中(web项目放到WEB-INF\lib目录下) *  * @author 单红宇 * */public class ConvertToPdf {	// WORD 转换文档格式参数值:17为pdf,8为html,2为txt(支持的格式不限与此,其他格式暂为列出)	static final int wdFormatPDF = 17;// PDF 格式	static final int wdFormatHTML = 8;// HTML 格式	static final int wdFormatTXT = 2;// TXT 格式	/**	 * Word文档转换	 * 	 * @param fromfileName	 * @param toFileName	 * @author SHANHY	 */	public void wordConvert(String fromfileName, String toFileName) {		System.out.println("启动Word...");		ComThread.InitSTA();  				long start = System.currentTimeMillis();		ActiveXComponent app = null;		Dispatch doc = null;		try {			app = new ActiveXComponent("Word.Application");//创建一个word对象			app.setProperty("Visible", new Variant(false)); //不可见打开word			app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏			Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性						System.out.println("打开文档 >>> " + fromfileName);			//Object[]第三个参数是表示“是否只读方式打开”			doc = Dispatch.invoke(docs, "Open", Dispatch.Method, 					new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch();			File tofile = new File(toFileName);			if (tofile.exists()) {				tofile.delete();			}			int formatValue = -1;			if(toFileName.toLowerCase().endsWith(".pdf")){				formatValue = wdFormatPDF;			}else if(toFileName.toLowerCase().endsWith(".html")){				formatValue = wdFormatHTML;			}else if(toFileName.toLowerCase().endsWith(".txt")){				formatValue = wdFormatTXT;			}else{				formatValue = -1;			}			if(formatValue != -1){				System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); 				Dispatch.invoke(doc, "SaveAs", Dispatch.Method, 						new Object[] { toFileName, new Variant(formatValue) }, new int[1]);			}else{				System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); 			}						long end = System.currentTimeMillis();						System.out.println("用时:" + (end - start) + "ms.");		} catch (Exception e) {			e.printStackTrace();			System.out.println("========Error:文档转换失败:" + e.getMessage());		} finally {			Dispatch.call(doc, "Close", false);			System.out.println("关闭文档");			if (app != null)				app.invoke("Quit", new Variant[] {});		}		// 如果没有这句话,winword.exe进程将不会关闭		ComThread.Release();          ComThread.quitMainSTA();  	}		/**	 * PPT(PowerPoint)文档转换	 * 	 * @param fromfileName	 * @param toFileName	 * @author SHANHY	 */	public void pptConvert(String fromfileName, String toFileName) {		System.out.println("启动PPT...");		ComThread.InitSTA();  				long start = System.currentTimeMillis();		ActiveXComponent app = null;		Dispatch doc = null;		try {			app = new ActiveXComponent("Word.Application");//创建一个word对象			app.setProperty("Visible", new Variant(false)); //不可见打开word			app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏			Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性						System.out.println("打开文档 >>> " + fromfileName);			//Object[]第三个参数是表示“是否只读方式打开”			doc = Dispatch.invoke(docs, "Open", Dispatch.Method, 					new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch();			File tofile = new File(toFileName);			if (tofile.exists()) {				tofile.delete();			}			int formatValue = -1;			if(toFileName.toLowerCase().endsWith(".pdf")){				formatValue = wdFormatPDF;			}else if(toFileName.toLowerCase().endsWith(".html")){				formatValue = wdFormatHTML;			}else if(toFileName.toLowerCase().endsWith(".txt")){				formatValue = wdFormatTXT;			}else{				formatValue = -1;			}			if(formatValue != -1){				System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); 				Dispatch.invoke(doc, "SaveAs", Dispatch.Method, 						new Object[] { toFileName, new Variant(formatValue) }, new int[1]);			}else{				System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); 			}						long end = System.currentTimeMillis();						System.out.println("用时:" + (end - start) + "ms.");		} catch (Exception e) {			e.printStackTrace();			System.out.println("========Error:文档转换失败:" + e.getMessage());		} finally {			Dispatch.call(doc, "Close", false);			System.out.println("关闭文档");			if (app != null)				app.invoke("Quit", new Variant[] {});		}		// 如果没有这句话,winword.exe进程将不会关闭		ComThread.Release();  		ComThread.quitMainSTA();  	}	public static void main(String[] args) {		ConvertToPdf d = new ConvertToPdf();		d.wordConvert("g:\\test.docx", "g:\\test.pdf");	}}

读、写Word的简单示例

import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Variant; import com.jacob.com.Dispatch; public class Word {     String strDir = "c:jacob_1.9";     String strInputDoc = strDir + "file_in.doc";     String strOutputDoc = strDir + "file_out.doc";     String strOldText = "[label:import:1]";     String strNewText =             "I am some horribly long sentence, so long that [insert anything]";     boolean isVisible = true;     boolean isSaveOnExit = true;     public Word() {         ActiveXComponent oWord = new ActiveXComponent("Word.Application");         oWord.setProperty("Visible", new Variant(isVisible));         Dispatch oDocuments = oWord.getProperty("Documents").toDispatch();         Dispatch oDocument = Dispatch.call(oDocuments, "Open", strInputDoc).                              toDispatch();         Dispatch oSelection = oWord.getProperty("Selection").toDispatch();         Dispatch oFind = oWord.call(oSelection, "Find").toDispatch();         Dispatch.put(oFind, "Text", strOldText);         Dispatch.call(oFind, "Execute");         Dispatch.put(oSelection, "Text", strNewText);         Dispatch.call(oSelection, "MoveDown");         Dispatch.put(oSelection, "Text",                      "nSo we got the next line including BR.n");         Dispatch oFont = Dispatch.get(oSelection, "Font").toDispatch();         Dispatch.put(oFont, "Bold", "1");         Dispatch.put(oFont, "Italic", "1");         Dispatch.put(oFont, "Underline", "0");         Dispatch oAlign = Dispatch.get(oSelection, "ParagraphFormat").                           toDispatch();         Dispatch.put(oAlign, "Alignment", "3");         Dispatch oWordBasic = (Dispatch) Dispatch.call(oWord, "WordBasic").                               getDispatch();         Dispatch.call(oWordBasic, "FileSaveAs", strOutputDoc);         Dispatch.call(oDocument, "Close", new Variant(isSaveOnExit));         oWord.invoke("Quit", new Variant[0]);     }     public static void main(String[] args) {         Word word = new Word();     } } 

4、jacob.jar的结构

jacob包括两个部分:

    com.jacob.activeX: ActiveXComponent类
    com.jacob.com: 其它类和元素

5、Jacob类

Jacob的结构很简单,包含以下几个类:

    ActiveXComponent Class:封装了Dispatch对象,用于创建一个封装了COM组件对象的Java Object
    Dispatch Class:用于指向封装后的MS数据结构。常用的方法有call,subcall,get,invoke…后面会介绍使用方法。
    Variant Class:用于映射COM的Variant数据类型。提供Java和COM的数据交换。

ComException Class:异常类

6、Jacob方法

用于访问COM/DLL对象的方法,读取、修改COM/DLL对象的属性。

    call method:属于Dispatch类。用于访问COM/DLL对象的方法。方法进行了重载,方便不同场合调用。返回一个Variant类型的值。
    callSub method:使用方法和call一样,不过它不返回值。
    get method:读取COM对象的属性值,返回一个Variant类型值。
    put method:设置COM对象的属性值。
    invoke method:call的另一种用法,更复杂一些。
    invokesub method:subcall的另一种用法
    getProperty method:属于ActiveXComponent类,读取属性值,返回一个Variant类型值。

setProperty method:属于ActiveXComponent类,设置属性值。

要注意一点:在使用Jacob时,很重要的一点是,用户必须安装有Office的应用程序。否则也就无法建立Java-COM桥,进而无法解析了。


部分内容参考: http://www.cnblogs.com/vulcans/archive/2009/09/08/1562588.html


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn