What is Jacob?
Jacob is an open source project it allows the COM interface Automation component to be called in Java, which uses JNI (local calling program) to invoke the COM library locally. It can be run in x86 and environments that support both 32-bit and 64-bit Java Virtual machines X64 (removed from Baidu entry).
Preparatory work:
Required jar Package: Jacob.jar, also need to put Jacob.dll in Windows/system32 directory, otherwise will error could not initialize class Com.jacob.activeX.ActiveXComponent.
Instance
Packages that need to be imported
Import com.jacob.activeX.ActiveXComponent; Import Com.jacob.com.Dispatch; import com.jacob.com.Variant;
First, Word to PDF
Public Static voidwordtopdf (String source,string tofile) {//This method only works with version 2003 of Word (it is said that Jacob can only do this, not personally)
TXT format files can also be converted to PDF using this method (pro-test can be successful, PS: Accidental discovery)
String filename=source; String Tofilename=ToFile; //System.out.println ("Start Word"); LongStart =System.currenttimemillis (); Activexcomponent app=NULL; Try { //Start the Word application (Microsoft Office Word 2003)App =NewActivexcomponent ("Word.Application"); ////set the Word application not to be visibleApp.setproperty ("Visible",false); //documents represents all document windows for a Word program (Word is a multi-document application)Dispatch docs = app.getproperty ("Documents")). Todispatch (); //System.out.println ("open document" + filename); //Open the Word file you want to convert to a temporary file in PDF formatDispatch doc = Dispatch.call (docs,//"Open",//FileName//FileName false,//confirmconversions true //ReadOnly). Todispatch (); //System.out.println ("Convert Document to PDF:" + tofilename);File ToFile =NewFile (tofilename); if(Tofile.exists ()) {tofile.delete (); } dispatch.call (Doc,//"SaveAs",//Tofilename,//FileNamewdformatpdf); //Close Word fileDispatch.call (Doc, "Close",false); LongEnd =System.currenttimemillis (); System.out.println (The conversion is complete. Spents: "+ (End-start) +" Ms. "); } Catch(Exception e) {System.out.println ("========error: Document conversion failed:" +e.getmessage ()); } finally { if(App! =NULL) //Close the Word applicationApp.invoke ("Quit", wdDoNotSaveChanges); } }
Second, Word to txt
Public Static voidword2txt (String inputfile, String outputFile) {BooleanFlag =false;//Open the Word applicationActivexcomponent app =NewActivexcomponent ("Word.Application"); Try { //set Word not visibleApp.setproperty ("Visible",NewVariant (false)); //Open Word fileDispatch Doc1 = App.getproperty ("Documents")). Todispatch (); Dispatch doc2= Dispatch.invoke (Doc1, "Open", Dispatch.method,NewObject[] {inputfile,NewVariant (false),NewVariant (true) },New int[1]). Todispatch (); //Save to temp file as txt formatDispatch.invoke (DOC2, "SaveAs", Dispatch.method,Newobject[] {outputFile,NewVariant (7)},New int[1]); //Close WordVariant f =NewVariant (false); Dispatch.call (DOC2,"Close", F); Flag=true; } Catch(Exception e) {e.printstacktrace (); } finally{App.invoke ("Quit",Newvariant[] {}); } }
Third, read word content (first convert Word to TXT, then read the TXT file)
Public Staticstring Readword (String path) {string Pathto= Path.replace ("Doc", "TXT"); Word2txt (Path,pathto); StringBuffer StringBuffer=NewStringBuffer (); File File=NewFile (Pathto); Try{FileInputStream fis=Newfileinputstream (file); byte[] buffer =New byte[1000]; intLen; Try { while( -1! = (len=fis.read (buffer))) {String temp=NewString (Buffer,0,len, "gb2312"); Stringbuffer.append (temp); } //System.out.println (stringbuffer.tostring ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } if(File.isfile ()) File.delete (); returnstringbuffer.tostring (); }
Java Operation Word (Jacob)