Java implementation MSOffice document conversion to PDF document

Source: Internet
Author: User

This document implements the ability to convert a Word document file into a PDF-formatted document

First step: Download the ZIP package

(1) Jacob's official website download Jacob Compress Package

(2) website:http://sourceforge.net/projects/jacob-project/

Step two : Configure the environment and import the jar Package

(1) unzip the downloaded compressed package

(2) Import the Jacob.jar package into the project

(3) copy and paste the jacob-1.18-m2-x64.dll and jacob-1.18-m2-x86.dll files into JDK installation directory bin directory, JDK installation directory JRE of the bin directory, the system disk C:\Windows\System32 directory.

Step three: Call The static method of the Msoffice2pdf class to implement the MsOffice document conversion.

Source code for the Msoffice2pdf class

1  PackageCom.bjsxt.shangxiaofei;2 3 Importcom.jacob.activeX.ActiveXComponent;4 ImportCom.jacob.com.Dispatch;5 6  Public classMsoffice2pdf {7     8     /**9 * Convert a Word document of the specified path to a PDF document of the specified pathTen * The path here is the absolute path One * @Title: Word2pdf A * @Description: TODO (here is a word describing the effect of this method) -     * @paramInputfile -     * @paramPdffile the     * @returnvoid return type -     * @authorShang - * @date 2014-8-15 a.m. 10:25:47 -      */ +      Public Static voidword2pdf (String inputfile,string pdffile) { -         //Open the Word application +Activexcomponent app =NewActivexcomponent ("Word.Application"); A         //set Word not visible atApp.setproperty ("Visible",false); -         //get all open documents in Word, return to documents object -Dispatch docs = app.getproperty ("Documents")). Todispatch (); -         //call the Open method in the documents object to open the document and return the Document object that opens -Dispatch doc =dispatch.call (Docs, -"Open", in Inputfile, -                                     false, to                                     true + ). Todispatch (); -         //Call the SaveAs method of the Document object to save the documents in PDF format the         /* * Dispatch.call (Doc, $ "SaveAs",Panax Notoginseng Pdffile, - wdformatpdf//word saved as PDF macro with a value of the                     ); +                     */ A Dispatch.call (Doc, the"ExportAsFixedFormat", + Pdffile, -17//Word saves a macro in PDF format with a value of $                 ); $          -          -         //Close Document theDispatch.call (Doc, "Close",false); -         //Close the Word applicationWuyiApp.invoke ("Quit", 0); the          -     } Wu      -      About  $}
View Code

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.