


Use jacob to call the COM object of Windows and convert Office files to pdf, html, etc._html/css_WEB-ITnose
Jun 24, 2016 am 11:50 AM
1、介紹
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、轉(zhuǎn)換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 轉(zhuǎn)換文檔格式參數(shù)值: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文檔轉(zhuǎn)換 * * @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");//創(chuàng)建一個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[]第三個參數(shù)是表示“是否只讀方式打開” 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("轉(zhuǎn)換文檔 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("轉(zhuǎn)換文件到目標文檔不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用時:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文檔轉(zhuǎn)換失?。?quot; + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("關(guān)閉文檔"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果沒有這句話,winword.exe進程將不會關(guān)閉 ComThread.Release(); ComThread.quitMainSTA(); } /** * PPT(PowerPoint)文檔轉(zhuǎn)換 * * @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");//創(chuàng)建一個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[]第三個參數(shù)是表示“是否只讀方式打開” 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("轉(zhuǎn)換文檔 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("轉(zhuǎn)換文件到目標文檔不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用時:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文檔轉(zhuǎn)換失?。?quot; + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("關(guān)閉文檔"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果沒有這句話,winword.exe進程將不會關(guān)閉 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的結(jié)構(gòu)
jacob包括兩個部分:
??? com.jacob.activeX: ActiveXComponent類
??? com.jacob.com: 其它類和元素
5、Jacob類
Jacob的結(jié)構(gòu)很簡單,包含以下幾個類:
??? ActiveXComponent Class:封裝了Dispatch對象,用于創(chuàng)建一個封裝了COM組件對象的Java Object
??? Dispatch Class:用于指向封裝后的MS數(shù)據(jù)結(jié)構(gòu)。常用的方法有call,subcall,get,invoke…后面會介紹使用方法。
??? Variant Class:用于映射COM的Variant數(shù)據(jù)類型。提供Java和COM的數(shù)據(jù)交換。
ComException Class:異常類
6、Jacob方法
用于訪問COM/DLL對象的方法,讀取、修改COM/DLL對象的屬性。
??? call method:屬于Dispatch類。用于訪問COM/DLL對象的方法。方法進行了重載,方便不同場合調(diào)用。返回一個Variant類型的值。
??? callSub method:使用方法和call一樣,不過它不返回值。
??? get method:讀取COM對象的屬性值,返回一個Variant類型值。
??? put method:設(shè)置COM對象的屬性值。
??? invoke method:call的另一種用法,更復雜一些。
??? invokesub method:subcall的另一種用法
??? getProperty method:屬于ActiveXComponent類,讀取屬性值,返回一個Variant類型值。
setProperty method:屬于ActiveXComponent類,設(shè)置屬性值。
要注意一點:在使用Jacob時,很重要的一點是,用戶必須安裝有Office的應(yīng)用程序。否則也就無法建立Java-COM橋,進而無法解析了。
部分內(nèi)容參考: http://www.cnblogs.com/vulcans/archive/2009/09/08/1562588.html

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The key to keep up with HTML standards and best practices is to do it intentionally rather than follow it blindly. First, follow the summary or update logs of official sources such as WHATWG and W3C, understand new tags (such as) and attributes, and use them as references to solve difficult problems; second, subscribe to trusted web development newsletters and blogs, spend 10-15 minutes a week to browse updates, focus on actual use cases rather than just collecting articles; second, use developer tools and linters such as HTMLHint to optimize the code structure through instant feedback; finally, interact with the developer community, share experiences and learn other people's practical skills, so as to continuously improve HTML skills.

The reason for using tags is to improve the semantic structure and accessibility of web pages, make it easier for screen readers and search engines to understand page content, and allow users to quickly jump to core content. Here are the key points: 1. Each page should contain only one element; 2. It should not include content that is repeated across pages (such as sidebars or footers); 3. It can be used in conjunction with ARIA properties to enhance accessibility. Usually located after and before, it is used to wrap unique page content, such as articles, forms or product details, and should be avoided in, or in; to improve accessibility, aria-labeledby or aria-label can be used to clearly identify parts.

To create a basic HTML document, you first need to understand its basic structure and write code in a standard format. 1. Use the declaration document type at the beginning; 2. Use the tag to wrap the entire content; 3. Include and two main parts in it, which are used to store metadata such as titles, style sheet links, etc., and include user-visible content such as titles, paragraphs, pictures and links; 4. Save the file in .html format and open the viewing effect in the browser; 5. Then you can gradually add more elements to enrich the page content. Follow these steps to quickly build a basic web page.

To create an HTML checkbox, use the type attribute to set the element of the checkbox. 1. The basic structure includes id, name and label tags to ensure that clicking text can switch options; 2. Multiple related check boxes should use the same name but different values, and wrap them with fieldset to improve accessibility; 3. Hide native controls when customizing styles and use CSS to design alternative elements while maintaining the complete functions; 4. Ensure availability, pair labels, support keyboard navigation, and avoid relying on only visual prompts. The above steps can help developers correctly implement checkbox components that have both functional and aesthetics.

To reduce the size of HTML files, you need to clean up redundant code, compress content, and optimize structure. 1. Delete unused tags, comments and extra blanks to reduce volume; 2. Move inline CSS and JavaScript to external files and merge multiple scripts or style blocks; 3. Simplify label syntax without affecting parsing, such as omitting optional closed tags or using short attributes; 4. After cleaning, enable server-side compression technologies such as Gzip or Brotli to further reduce the transmission volume. These steps can significantly improve page loading performance without sacrificing functionality.

It is a semantic tag used in HTML5 to define the bottom of the page or content block, usually including copyright information, contact information or navigation links; it can be placed at the bottom of the page or nested in, etc. tags as the end of the block; when using it, you should pay attention to avoid repeated abuse and irrelevant content.

HTMLhasevolvedsignificantlysinceitscreationtomeetthegrowingdemandsofwebdevelopersandusers.Initiallyasimplemarkuplanguageforsharingdocuments,ithasundergonemajorupdates,includingHTML2.0,whichintroducedforms;HTML3.x,whichaddedvisualenhancementsandlayout

ThetabindexattributecontrolshowelementsreceivefocusviatheTabkey,withthreemainvalues:tabindex="0"addsanelementtothenaturaltaborder,tabindex="-1"allowsprogrammaticfocusonly,andtabindex="n"(positivenumber)setsacustomtabbing
