Similar to Baidu Library solution (iv) -- use JODConverter to call OpenOffice.org service to convert the document into a PDF. Use Jacob to call MS Office to convert the document into a PDF file. Use JCom to call MS Offic.

Source: Internet
Author: User
Tags api manual
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. JODConverter Introduction

JODConverter is a Java OpenDocument file converter that can convert many file formats. It relies on services provided by OpenOffice.org or LibreOffice for conversion. It can convert Microsoft Office documents (Word, Excel, and PowerPoint) to PDF format.

You can embed JODConverter in a Java application, or use it as a command line and a script to call it. You can also use Web applications or Web Services for network applications.

(1) Conversion document example:

View Code

OfficeManager officeManager = new DefaultOfficeManagerConfiguration (). buildOfficeManager (); // start the OpenOffice service officeManager. start (); // execute the conversion OfficeDocumentConverter converter = new OfficeDocumentConverter (officeManager); converter. convert (new File ("test. odt "), new file({test}"); // stop the service officeManager. stop ();

(2) main categories:

OfficeManager is an interface that defines three methods:

  • Public void start () starts the OpenOffice Service
  • Public void stop () to stop the OpenOffice Service
  • Public void execute (OfficeTask task) to execute the conversion task

DefaultOfficeManagerConfiguration is an entity class that implements the OfficeManager interface. It provides related methods to configure OpenOffice.org, such:

Public DefaultOfficeManagerConfiguration setOfficeHome (String officeHome) sets the OpenOffice.org or LibreOffice installation directory. In windows, the default value is "C: \ Program Files \ OpenOffice.org 3" (LibreOffice changes accordingly ), therefore, if OpenOffice.org is installed in another directory, you must set this item.

Public DefaultOfficeManagerConfiguration setConnectionProtocol (OfficeConnectionProtocol conn) sets the connection protocol to determine whether to use pipe communication or socekt communication.

Pubcli DefaultOfficeManagerConfiguration setTemplateProfileDir (File templateProfileDir) sets the temporary directory.

In addition to the preceding methods, defaultofficemanagerconfiguration also provides other OpenOffice.org configuration methods. You can query the jodconverter API manual for specific methods. After the configuration, you must execute the buildofficemanager () method to implement the real configuration.

Officedocumentconverter mainly includes the convert method, which actually calls the execute method in the class that implements the officemanager interface.

2. Use JODConverter to convert documents to PDF format to implement 2.1 Algorithm Design

The algorithm flowchart is shown in Figure 4-1:

Figure 4-1 jodconverter calls the OpenOffice service to convert the document to a PDF Algorithm

Step 1: Start the OpenOffice service. For example, first set the defaultofficemanagerconfiguration parameters to configure OpenOffice and obtain the officemanager. Call the start method of the officemanager implementation class to start the service.

Step 2: Convert the document. For example, initialize officedocumentconverter and call its convert method to execute the conversion.

Step 3: Stop the OpenOffice service. For example, call the officemanager method to stop the stop method of the class.

2.2 install and configure

Jodconverter is an open-source document conversion tool that can be applied to both Linux and Windows platforms. It is based on OpenOffice.org or libreoffice. Therefore, OpenOffice or libreoffice must be installed on the document conversion server.

Currently, jodconverter of the latest version isJODConverter3.0It requires the Java environment above JDK and OpenOffice.org 3.x. This article is based on the design implementation of the latest version 3.0. if the version is 2, there are different implementations. (Version 2 needs to manually start OpenOffice.org, or create a Windows service to enable it. Version 3 provides an interface to enable the Service. Therefore, we use version 3)

2.3 implement Code View Code

Package com.converterconverter Converter; import java. io. file; import java. io. fileNotFoundException; import org. artofsolving. jodconverter. officeDocumentConverter; import org. artofsolving. jodconverter. office. defaultOfficeManagerConfiguration; import org. artofsolving. jodconverter. office. officeManager; import com. converter. utils. fileUtils; public class openoffice‑converter implements converter {private static OfficeManager officeManager; private static String OFFICE_HOME = "D :\\ Program Files \ OpenOffice.org 3"; private static int port [] = {8100}; public void convert2PDF (String inputFile, string pdfFile) {if (inputFile. endsWith (". txt ") {String odtFile = FileUtils. getFilePrefix (inputFile) + ". odt "; if (new File (odtFile ). exists () {System. out. println ("The odt file already exists! "); InputFile = odtFile;} else {try {FileUtils. copyFile (inputFile, odtFile); inputFile = odtFile;} catch (FileNotFoundException e) {System. out. println ("the document does not exist! "); E. printStackTrace () ;}} startService (); System. out. println ("conversion and conversion of documents:" + inputFile + "-->" + pdfFile); OfficeDocumentConverter converter = new OfficeDocumentConverter (officeManager); converter. convert (new File (inputFile), new File (pdfFile); stopService (); System. out. println ();} public void convert2PDF (String inputFile) {String pdfFile = FileUtils. getFilePrefix (inputFile) + ". pdf "; convert2 PDF (inputFile, pdfFile);} public static void startService () {DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration (); try {System. out. println ("prepare to start the service .... "); configuration. setOfficeHome (OFFICE_HOME); // set the OpenOffice.org installation directory configuration. setPortNumbers (port); // set the conversion port. The default value is 8100 configuration. setTaskExecutionTimeout (1000*60 * 5L); // set the task execution timeout to 5 minutes. setTaskQ UeueTimeout (1000*60*60 * 24L); // set the task queue timeout to 24 hours officeManager = configuration. buildOfficeManager (); officeManager. start (); // start the service System. out. println ("office conversion service started successfully! ");} Catch (Exception ce) {System. out. println (" An error occurred while starting the office conversion service! Details: "+ ce) ;}} public static void stopService () {System. out. println (" Disable office conversion service... "); if (officeManager! = Null) {officeManager. stop ();} System. out. println ("The office conversion is disabled successfully! ");}}

 

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.