search
HomeWeb Front-endHTML TutorialUse jacob to call the COM object of Windows and convert Office files to pdf, html, etc._html/css_WEB-ITnose

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
HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!