Java converts word to PDF with OpenOffice

Source: Internet
Author: User

Java converts word to PDF with OpenOffice

First, Software Installation and Jar Package Download

The official website is as follows (English):

OpenOffice http://www.openoffice.org/

Jodconverter http://sourceforge.net/projects/jodconverter/files/JODConverter/

You can also Baidu to search, on the csdn above can also download to the required package.

jodconverter:http://download.csdn.net/download/yali1990515/4443791

After decompression, all the jar packages under the directory are placed under the project Lib or referenced in a way that invokes these jar packages.

OpenOffice:

Http://www.baidu.com/link?url= Nprmkalzg44nn-spouwjsrrlgohs5qzn7aejtc2ccsukjjwo6rxfjhh5g4kw0ckzm4zjs2kr2grdwsq8tarcswjb08oujq_ih4wqeeml_lm

Install after download, I install the path to C:/openoffice 4

Second, Start the service

This part will be implemented as thread-initiated in the program, without the need for additional cmd windows to invoke the system to start the service.

The implementation starts the service and shuts down the service after all operations are completed. In other words, each execution of the program goes through the process of service startup and service shutdown. If you think this is an impact on system performance, you can also start the OpenOffice service separately.

Third, Program Code

Package Com.sheke.wenku;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import Com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import Com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

Public class office2pdf {

/**

* Convert Office documents to PDF. Running this function requires the use of OpenOffice, OpenOffice

* http://www.openoffice.org/

*

* <pre>

* Method Example:

* String SourcePath = "F:\\office\\source.doc";

* String destfile = "f:\\pdf\\dest.pdf";

* Converter.office2pdf (SourcePath, destfile);

* </pre>

*

* @param sourcefile

* source file, absolute path. It can be a document of office2003-2007 in all formats, Office2010 not tested. including. Doc,

*            . docx,. xls,. xlsx,. ppt,. pptx and so on. Example: F:\\office\\source.doc

* @param destfile

* Target file. Absolute path. Example: f:\\PDF\\dest.pdf

* @return Prompt for the success or absence of the operation. If 1 is returned, the source file is not found, or the url.properties configuration error; If you return 0,

* Indicates the operation is successful; Returns 1, which indicates that the conversion failed

*/

Public Static int office2pdf (string sourcefile, String destfile) {

Try {

File Inputfile = new file (sourcefile);

if (!inputfile.exists ()) {

return -1;//The source file is not found, returns-1

}

If the destination path does not exist, the path is created

File OutputFile = new file (destfile);

if (!outputfile.getparentfile (). exists ()) {

Outputfile.getparentfile (). Mkdirs ();

}

Start the OpenOffice service

String command = "C:/openoffice 4/program/soffice.exe-headless-accept=\" socket,host=127.0.0.1,port=8100;urp;\ "";

Process Pro = Runtime. GetRuntime (). exec (command);

Connect to a OpenOffice.org instance running on port 8100

Openofficeconnection connection = new socketopenofficeconnection ("127.0.0.1", 8100);

Connection.connect ();

Convert

Documentconverter converter = new openofficedocumentconverter (

connection);

Converter.convert (Inputfile, outputFile);

Close the connection

Connection.disconnect ();

Shutting down the process of the OpenOffice service

Pro.destroy ();

return 0;

} catch (FileNotFoundException e) {

E.printstacktrace ();

return -1;

} catch (IOException e) {

E.printstacktrace ();

}

return 1;

}

Public Static void Main (string[] args) {

String Source = "D:\\trs/trswcmv7\\protect\\p0201501\\p020150109\\p020150109485061451482.doc";

String dest = "C:\\1.pdf";

int result = office2pdf(source, dest);

System. out. println (result);

}

}

You can comment out the annotations appropriately.

Four, Common Errors

Error message:

Java.net.ConnectException:connection Failed:socket,host=10.101.50.71,port=8100,tcpnodelay=1: Java.net.ConnectException:Connection Refused:connect

At Com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection.connect ( abstractopenofficeconnection.java:79)

At COM.YGSOFT.EAM.DEMO.DOCCONVERTER.WORDTOPDF (docconverter.java:65)

At Com.ygsoft.eam.demo.DocConverter.conver (docconverter.java:156)

At Com.ygsoft.eam.demo.DocConverter.main (docconverter.java:206)

Java.lang.NullPointerException

At Com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection.disconnect ( ABSTRACTOPENOFFICECONNECTION.JAVA:88)

At COM.YGSOFT.EAM.DEMO.DOCCONVERTER.WORDTOPDF (docconverter.java:72)

At Com.ygsoft.eam.demo.DocConverter.conver (docconverter.java:156)

At Com.ygsoft.eam.demo.DocConverter.main (docconverter.java:206)

Cause: The newly installed OpenOffice has not been registered, and when you use the program to call Soffice.exe will be blocked, and then connection refused error.

Workaround: Find this Soffice.exe program installed on the computer, double-click Open, after registration can be used normally.

Java converts word to PDF with OpenOffice

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.