The public class Dispatch_msword {static final int wddonotsavechanges = 0;//does not save pending changes. static final int wdformatpdf = 17;//PDF format public void ofseventopdf (String sfilename,string tofilename) {//strin
G filename = "d:\\ Supplemental confidentiality agreement. Doc";
String tofilename = sFileName + ". pdf";
System.out.println ("Start Word ...");
Long start = System.currenttimemillis ();
Activexcomponent app = null;
try {app = new activexcomponent ("Word.Application");
App.setproperty ("Visible", false);
Dispatch docs = app.getproperty ("Documents"). Todispatch ();
SYSTEM.OUT.PRINTLN ("Open Document ..." + sfilename);
Dispatch doc = Dispatch.call (docs,//"Open",//sfilename,//FileName false,//confirmconversions True//ReadOnly). To
Dispatch ();
SYSTEM.OUT.PRINTLN ("Convert Document to PDF ..." + tofilename); File ToFile = new file (tofilename);
if (tofile.exists ()) {tofile.delete ();
} dispatch.call (doc,//"SaveAs",//Tofilename,//FileName
Wdformatpdf);
Dispatch.call (Doc, "Close", false);
Long end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("conversion complete.")
Spents: "+ (End-start) +" Ms. ");
catch (Exception e) {System.out.println ("========error: Document conversion failed:" + e.getmessage ());
Finally {if (app!= null) app.invoke ("Quit", wdDoNotSaveChanges); }
}
To run this code, you need to have a plug-in in Word on the machine that contains the Save as PDF