Import Java.io.File;
Import com.jacob.activeX.ActiveXComponent;
Import Com.jacob.com.Dispatch;
public class Word2pdf
{
public static void Main (String args[])
{
Activexcomponent app = null;
String wordfile = "C:/xxxxx.doc";
String pdffile = "C:/xxxxx.pdf";
System.out.println ("Start conversion ...");
Long start = System.currenttimemillis ();
Try
{
Open Word
App = new Activexcomponent ("Word.Application");
App.setproperty ("Visible", false);
Get all open documents in Word
Dispatch documents = App.getproperty ("Documents"). Todispatch ();
System.out.println ("Open file:" + wordfile);
Open Document
Dispatch document = Dispatch.call (documents, "Open", Wordfile, False, True). Todispatch ();
If the file exists, does not overwrite, will be directly error, so we need to determine whether the file exists
File target = new file (pdffile);
if (Target.exists ())
{
Target.delete ();
}
System.out.println ("Save as:" + pdffile);
Save As, error document to PDF, where Word saved as pdf The value of the macro is 17
Dispatch.call (document, "SaveAs", Pdffile, 17);
Close Document
Dispatch.call (document, "Close", false);
End time
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Conversion successful, Time:" + (End-start) + "MS");
}
catch (Exception e)
{
System.out.println ("Conversion failed" + e.getmessage ());
}
Finally
{
Close Office
App.invoke ("Quit", 0);
}
}
}
Jar package Jacob.jar available for download in Http://sqdownb.onlinedown.net/down/74195_20170622162225.zip
Java to convert Word files to PDF