Software interoperability is a problem we often face. If Java can freely call the mature code of other languages and platforms, you can make full use of your Java skills and greatly improve your productivity. The existing Java com interoperability technology has many implementations. The Jacob open-source project provides a simple and convenient universal call framework.
The open-source project of Jacob provides a JVM independent Automation server implementation. Its core is the variant, dispatch, and other interfaces based on the JNI technology, the design references Microsoft VJ ++'s built-in General Automation server, but Microsoft's implementation only supports its own JVM. With Jacob, you can easily call the late binding method in Java.
The following uses Jacob to convert Word to HTML.
1· Configure the development and running environment.
First, to use Jacob to "bridge", You need to download the latest Jacob-related class libraries, including Jacob. jar and Jacob. dll.
(1) Add the JAR file to the build path of your Eclipse project.
(2) You need to add the DLL file to the PATH environment variable of the system, or you can set the PATH variable at runtime in the program. A simple "bridge" has been built up, and now you can easily call the com class library you are familiar. You must register the COM component in the operating system. You can use the installer provided by the redemption toolkit, or use the regsvr32 tool that comes with windows to register the DLL file.
The Java code is as follows:
Package test;
Import java. Io. file;
Import com. Jacob. ActiveX. activexcomponent;
Import com.jacb.com. Dispatch;
Import com.jacb.com. Variant;
Public class wordtohtml {
Public Boolean changeformat (string filename ){
String fileformat = filename. substring (filename. Length ()-4, filename. Length ());
System. Out. println ("file suffix" + fileformat );
If (fileformat. equalsignorecase (". Doc ")){
String docfile = filename;
System. Out. println ("Word file path:" + docfile );
// Full path of the Word file
String htmlfile = docfile. substring (0, (docfile. Length ()-4) + ". htm ";
System. Out. println ("HTM file path:" + htmlfile );
// Complete path of the HTML file
Activexcomponent APP = new activexcomponent ("word. application ");
// Start WORD
Try
{
App. setproperty ("visible", new variant (false ));
// Set the word program to run in non-visual mode
Dispatch docs = (dispatch) app. getproperty ("events"). todispatch ();
Dispatch Doc = dispatch. invoke (Docs, "open", dispatch. method, new object [] {docfile, new variant (false), new variant (true)}, new int [1]). todispatch ();
// Open the Word file
Dispatch. Invoke (Doc, "saveas", dispatch. method, new object [] {htmlfile, new variant (8)}, new int [1]);
// Save the file as an HTM format
Dispatch. Call (Doc, "close", new variant (false ));
// Close the file
}
Catch (exception e ){
E. printstacktrace ();
}
Finally {
App. Invoke ("quit", new variant [] {});
// Exit the word Program
}
// The conversion is complete.
Return true;
}
Else {
Return false;
}
}
Public static void main (string [] ARGs ){
Wordtohtml d = new wordtohtml ();
File F = new file ("C: \ a.doc ");
System. Out. println (F. getabsolutepath ());
System. Out. println (D. changeformat (F. getabsolutepath ()));
}
In this way, the conversion from word to HTML is completed.