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