Java to convert Word to PDF Jacob mode

Source: Internet
Author: User

Tag: off requires location x64 Ice component Sys under app

 Packagecom.doctopdf;ImportJava.io.File;Importcom.jacob.activeX.ActiveXComponent;ImportCom.jacob.com.ComThread;ImportCom.jacob.com.Dispatch;Importcom.jacob.com.Variant;/*** One of the best methods, but requires a window environment, and the speed is the slowest need to install Msofficeword * *:http://www.microsoft.com/zh-cn/download/details.aspx?id=7* Jacob's Bag:http://sourceforge.net/projects/jacob-project/* The extracted files * Jacob.jar placed in E:\jdk1.8.0_14\jre\lib\ext here using their own JDK installation location * Jacob.dll placed E:\jdk1.8.0_14\jre\bin here to make Install location with your own JDK * if not put or misplaced, an error will appear * Note: x86 refers to a 32-bit system * x64 refers to 64-bit system  * *@authoru Y * Support Doc docx file to PDF*/ Public classDoctopdf {Private Static Final intWdformatpdf = 17;//PDF format 17 means word to PDF 44 for execl to HTML     Public voidwordtopdf (String sfilename, String tofilename) {System.out.println ("Start Word ..."); LongStart = System.currenttimemillis ();//Conversion Start TimeActivexcomponent app =NULL; Dispatch Doc=NULL; Try{app=NewActivexcomponent ("Word.Application"); App.setproperty ("Visible",NewVariant (false)); Dispatch Docs= App.getproperty ("Documents")). Todispatch (); Doc= Dispatch.call (Docs, "Open", sFileName). Todispatch (); System.out.println ("Open Document ..." +sfilename); System.out.println ("Convert document to PDF ..." +tofilename); File ToFile=NewFile (tofilename); if(Tofile.exists ()) {tofile.delete (); }            //Save as HTML format to temp file: 17 means word to PDF 44 for execl to HTMLDispatch.call (Doc, "SaveAs", Tofilename,//FileNamewdformatpdf); LongEnd = System.currenttimemillis ();//Conversion End TimeSYSTEM.OUT.PRINTLN ("Conversion Complete: Spents: "+ (End-start) +" Ms. "); } Catch(Exception e) {System.out.println ("========error: Document conversion failed:" +e.getmessage ()); } finally{Dispatch.call (doc,"Close",false); System.out.println ("Close Document"); if(App! =NULL) App.invoke ("Quit",Newvariant[] {}); }        //without this remark, the Winword.exe process will not closecomthread.release (); }     Public Static voidMain (string[] args) {doctopdf d=Newdoctopdf (); D.wordtopdf ("C:\\users\\administrator\\desktop\\ Graduation design Table structure design. docx", "C:\\spring.pdf"); }}

Need to add Jacob.jar

Java to convert Word to PDF Jacob mode

Related Article

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.