Java implementation Word to PDF

Source: Internet
Author: User

Java implementation Word to PDF

Do not know why the online friends write so complex, they have seen a good, their own changes to change, testing can be used, I hope you can refer to, for everyone to help

1. Introduction of the JAR package

2. Code

1  Public Static voidWtopdfchange (String wordfile,string pdffile) {//wordfile Word path //pdffile PDF Path 2         3Activexcomponent app =NULL;4System.out.println ("Start conversion ...");5            //Start Time6           //Long start = System.currenttimemillis (); 7            Try {8             //Open Word9App =NewActivexcomponent ("Word.Application");Ten             //get all open documents in Word OneDispatch documents = App.getproperty ("Documents")). Todispatch (); ASystem.out.println ("Open file:" +wordfile); -             //Open Document -Dispatch document = Dispatch.call (documents, "Open", Wordfile,false,true). Todispatch (); the             //If the file exists, does not overwrite, will be directly error, so we need to determine whether the file exists -File target =NewFile (Pdffile);  -              if(Target.exists ()) { - Target.delete (); +              } -System.out.println ("Save as:" +pdffile); +Dispatch.call (document, "SaveAs", Pdffile, 17); A             //Close Document atDispatch.call (document, "Close",false); -}Catch(Exception e) { -System.out.println ("Conversion failed" +e.getmessage ()); -}finally { -              //Close Office -App.invoke ("Quit", 0); in            } -}

3. Test code

1  Public Static void Main (string[] args) {2         3         4         String Word = "d:/can be finished/java.docx"; 5         String name = "Zhukaixin". Concat (". pdf"); 6         String PDF = "d:\\" +name; 7         wtopdfchange (Word, pdf); 8     }

4. Run code results

hahaha welcome all Friends Spit Groove yo ha haha

Java implementation Word to PDF

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.