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