Java convert Word file to PDF

Source: Internet
Author: User

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

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.