Baidu Library solution (iii) -- using JCom to call MS Office or Acrobat API to convert documents into PDF; using Jacob to call MS Office to convert documents into PDF; using JODConverter to call Op

Source: Internet
Author: User
Directory

Baidu Library solution (I)-Overall Thinking

Baidu Library solution (2) -- use Jacob to call MS Office to convert the document to PDF

Baidu Library solution (3) -- use JCom to call MS Office or Acrobat API to convert the document to PDF

Similar to Baidu Library solution (4) -- use JODConverter to call OpenOffice.org to convert the document to PDF

Baidu Library solution (5)-use SWFTools to Convert PDF files to SWF

Baidu Library solution (6)-use FlexPaper to display Flash (SWF)

 

1. Introduction to JCom

JCom is an open-source project in Japan. It aims to allow Java programs to access COM components on the Windows platform, just like Jacob. With JCom, almost all COM objects can be controlled in Java, and it also has some powerful auxiliary classes for Excel operations.

Jcom mainly uses the ReleaseManager and IDispatch classes. The ReleaseManager class is equivalent to a container that manages COM components on the machine. With the IDispatch class, you can create COM component objects. For example, create a COM Object for an Excel application:

  ReleaseManager rmExcel = new ReleaseManager( );  IDispatch excelApp = new IDispatch(rmExcel,”Excel.Application”);

In addition, you need to call the release () method in the ReleaseManager class to release the resources of the created ReleaseManager at the end of the program to avoid memory overflow.

Similar to the Dispatch class in Jacob, the IDispatch class represents the COM Component Object and provides interfaces for accessing the attributes and methods of the Microsoft COM component object. Common IDispatch methods are as follows:

Get () method: get the properties of the COM Component Object and return a java. lang. Object Type value.

Put () method: Set the attributes of the COM component object without returning the value.

Invoke () method: Call the method of the COM Component Object and return a value of the java. lang. Object type. It is not recommended. We recommend that you use the method.

Method () method: The same as the invoke () method.

2. JCom calls the MS Office COM component to convert the Office document to ipv2.1. Prerequisites

JCom is an open-source project developed by Japanese people. Similar to Jacob, JCom is also used to call COM automation components. The running environment of JDK is also essential. JCom can also use Microsoft Office APIs to convert documents. Microsoft Office2007 or a later version is required, and Microsoft Save as PDF or XPS plug-in is still required for Office2007. In addition to calling Microsoft Office APIs to convert documents to PDF, JCom can also use the CreatePDF function of Adobe Acrobat to directly convert documents in Office format to PDF. Therefore, if you need to use Adobe Acrobat to convert documents, you need to install Adobe Acrobat again.

2.2 install and configure

Download the JCom package, decompress it, and extract the jcom. the jar package is configured in the Java project's class path, and the jcom. place the dll file in the System32 directory of the windows system. If you do not want to pollute the dll file in the System32 directory, you can also place it in the bin folder under the JDK installation directory of the local machine.

2.3 Implementation

The code for converting Word to PDF is as follows:

JCom calls MS Office to convert word to PDF source code

Public static void word2pdf (string inputfile, string pdffile) {releasemanager Rm = NULL; idispatch APP = NULL; try {Rm = new releasemanager (); APP = new idispatch (RM, "word. application "); // start the word app. put ("visible", false); // set the word invisible idispatch docs = (idispatch) app. get ("documents"); // obtain all open documents in the word idispatch Doc = (idispatch) docs. method ("open", new object [] {inputfile, false, true}); // open the document DOC. method ("saveas", new object [] {pdffile, 17}); // The conversion document is in PDF Format Doc. method ("close", new object [] {false}); app. method ("quit", null);} catch (exception e) {e. printstacktrace ();} finally {try {APP = NULL; RM. release (); Rm = NULL;} catch (exception e) {e. printstacktrace ();}}}
3. JCom calls Acrobat to convert Office documents to PDF Format

In addition to calling the MS Office conversion document to PDF method, you can also call the Adobe Acrobat API to convert the Office document to PDF format. below is the JCom implementation method, of course, you can also use Jacob to implement it, I will not study it here.

JCom calls Acrobat to convert the Office document to PDF

 1 public void convert2PDF(String inputFile, String pdfFile)  { 2          3  4         ReleaseManager rm = new ReleaseManager(); 5         IDispatch app; 6         try { 7             app = new IDispatch(rm, "PDFMakerAPI.PDFMakerApp"); 8             app.method("CreatePDF",new Object[]{inputFile,pdfFile}); 9             System.out.println("conversion completed!");10         } catch (JComException e) {11             e.printStackTrace();12         }finally{13              app=null;14              rm.release();15              rm = null;16         }   17       18     }

 

Note:

Source code: DocConverter

Reprinted Description: This article is the author's original. When reposted, please indicate the source of the article: Sean also has a dream blog Park. Please respect the author's Labor achievements. Thank you!

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.