Java calls COM components to convert Office files to PDF

Source: Internet
Author: User

In very many enterprise applications, it involves converting office images to PDFs for saving or publishing, because documents in PDF format facilitate encryption and permission control (similar to Baidu Library). Sum up now convert Office files to

There are two main ways to PDF:
1. Use Jcob to call MS Office's COM components to convert Office documents to PDF.


2. Use Jcob to call Acrobat to convert Office documents to PDF.

Found in actual use. Because office is different, the file format is inconsistent, so no third-party conversion tools can be perfectly compatible with all Office version numbers, and the best solution is to call the MS Office-provided COM components to

Row conversions. Even if the office high version number appears. All we have to do is just upgrade our office version number.

First, document conversion with the COM components of Office requires that you first need to download a Jacob.jar and jacob.dll,jacob.dll on the Jacob website to be placed below system32 windows. The Jacob.jar is referenced to the javaproject.

Second, write the code. Initializes the word process and returns all document objects for Word:
Open the Word application
Activexcomponent app = new Activexcomponent ("Word.Application");
Set word not to be visible, or the word interface will pop up
App.setproperty ("Visible", false);
Get all open documents in Word, return to documents object
Dispatch docs = app.getproperty ("Documents"). Todispatch ();

The third step. Open the Word document that we want to convert:
The Open method in the call Documents object opens the document. and returns an open Document object
Dispatch doc = Dispatch.call (docs,
"Open",
"Xxx.doc",
False
True
). Todispatch ();

Finally, save the document that we just opened. and close the word process:
Call the SaveAs method of the Document object to save the documents in PDF format
Dispatch.call (Doc,
"ExportAsFixedFormat",
"Xxx.pdf",
Wdformatpdf//word saved to PDF format
); Close Document
Dispatch.call (Doc, "Close", false);
Close the Word application
App.invoke ("Quit", 0);

Appendix: Complete Code

public static void Word2pdf (String inputfile,string pdffile) {
Open the Word application
Activexcomponent app = new Activexcomponent ("Word.Application");
Sets Word not to be visible. Otherwise the word interface will pop up
App.setproperty ("Visible", false);
Get all open documents in Word, return to documents object
Dispatch docs = app.getproperty ("Documents"). Todispatch ();
The Open method in the call Documents object opens the document. and returns an open Document object
Dispatch doc = Dispatch.call (docs,
"Open",
Inputfile,
False
True
). Todispatch ();
Invokes the SaveAs method of the Document object. Save a document in PDF format
Dispatch.call (Doc,
"ExportAsFixedFormat",
Pdffile,
Wdformatpdf//word saved to PDF format
);
Close Document
Dispatch.call (Doc, "Close", false);
Close the Word application
App.invoke ("Quit", 0);
}

Java calls COM components to convert Office 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.