Reference: http://www.cnblogs.com/qingxinblog/articles/3399454.html
1, Prerequisites: Server for Windows, must install Office using Jacob to make middleware call COM component Open Word process save document to other format
2, note the condition: Use eclipse must add Jacob's jar package to the project and the add build path, while placing Jacob's DLL file in Java_home\jre\bin, if the jar package is placed in the web_ for the deployment service The Lib directory under INF
The code is as follows:
1 Packagedemo;2 3 Importjava.io.IOException;4 Importjavax.servlet.ServletException;5 ImportJavax.servlet.http.HttpServlet;6 Importjavax.servlet.http.HttpServletRequest;7 ImportJavax.servlet.http.HttpServletResponse;8 Importcom.jacob.com.*;9 Importcom.jacob.activex.*;Ten One Public classHdzservletextendshttpservlet{ A - protected voidService (HttpServletRequest req, httpservletresponse Res) - throwsservletexception, IOException { theRes.getwriter (). println ("Ni hao hdz!"); -String docfile = "F:\\123.doc"; -String htmlfile = "F:\\123.pdf"; - hdzservlet.wordtohtml (Docfile, htmlfile); + - } + A Public Static Final intword_html = 5; at - /** - * Word to HTML - * @paramdocfile Word file full path - * @paramHtmlfile converted HTML storage path - */ in Public Static voidwordtohtml (String docfile, string htmlfile) - { to //Start the Word application (Microsoft Office Word 2003) +Activexcomponent app =NewActivexcomponent ("Word.Application"); -SYSTEM.OUT.PRINTLN ("* * * * * is converting ... * * * * * * * * *); the Try * { $ //set the Word application not to be visiblePanax NotoginsengApp.setproperty ("Visible",NewVariant (false)); - //documents represents all document windows for a Word program (Word is a multi-document application) theDispatch docs = app.getproperty ("Documents")). Todispatch (); + //Open the Word file you want to convert ADispatch doc =Dispatch.invoke ( the Docs, +"Open", - Dispatch.method, $ NewObject[] {docfile,NewVariant (false), $ NewVariant (true) },New int[1]). Todispatch (); - //Save to temp file as HTML format -Dispatch.invoke (Doc, "SaveAs", Dispatch.method,Newobject[] { theHtmlfile,NewVariant (word_html)},New int[1]); - //Close Word fileWuyiDispatch.call (Doc, "Close",NewVariant (false)); the } - Catch(Exception e) Wu { - E.printstacktrace (); About } $ finally - { - //Close the Word application -App.invoke ("Quit",Newvariant[] {}); A } +SYSTEM.OUT.PRINTLN ("* * * * conversion completed ********"); the } - $ the}
[JAVA] using Jacob for server-side word to HTML PDF