[JAVA] using Jacob for server-side word to HTML PDF

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.