Java converts word to PDF with OpenOffice

Source: Internet
Author: User

This article is based on the original text has been modified, please refer to the original text:

http://titanseason.iteye.com/blog/1471606 because this blog does not support attachment attachments please download here

http://my.oschina.NET/bigyuan/blog/165464

1. Required Software

OpenOffice http://www.openoffice.org/

Jodconverter http://sourceforge.net/projects/jodconverter/files/JODConverter/can also be downloaded directly from the attachment.

2. Start the OpenOffice service

After installing OpenOffice, install the service

CD C:\Program Files (x86) \openoffice 4\program

Perform

Soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"-nofirststartwizard

To see if the installation was successful, view the PID for the port

Netstat-ano|findstr "8100"

View the service program name for the PID

TASKLIST|FINDSTR "pid Value"

3. Add the Jodconverter related jar package to the project

4. The following is the implementation code

There are ready-to-use project examples in the attachment, which can be run directly into eclipse

1 This article is based on the original text has been modified, please refer to the original text:2 3http//titanseason.iteye.com/blog/1471606 Because this blog does not support attachment attachments please download here4http//my.oschina.net/bigyuan/blog/16546451the software that needs to be used6 7OpenOffice http://www.openoffice.org/8 9Jodconverter http://sourceforge.net/projects/jodconverter/files/jodconverter/can also be downloaded directly from the attachment.Ten  One    A2. Start the OpenOffice service -  -  the after installing OpenOffice, install the service -  -CD C:\Program Files (x86) \openoffice 4\program -  + Execution -  +Soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"-Nofirststartwizard A  at to see if the installation was successful, view the PID for the port -  -Netstat-ano|findstr "8100" -  - View the service program name for the PID -  inTASKLIST|FINDSTR "pid Value" -  to   +3. Add Jodconverter-related jar packages to your project -    the4. Here is the implementation code * there are ready-to-use project examples in the attachment, which can be run directly into Eclipse $ Panax Notoginseng [Java] View plain copy - /**  the * Convert Office documents to PDF. OpenOffice is required to run the function, OpenOffice is +      * http://www.openoffice.org/  A      *   the * <pre> + * Method Example: - * String SourcePath = "F:\\office\\source.doc"; $ * String destfile = "F:\\pdf\\dest.pdf"; $ * Converter.office2pdf (SourcePath, destfile); - * </pre> -      *   the      * @paramsourcefile - * source file, absolute path. Can be office2003-2007 in all formats, Office2010 not tested. Includes. doc,Wuyi *. docx,. xls,. xlsx,. ppt,. pptx, and so on. Example: F:\\office\\source.doc the      * @paramDestFile - * destination file. Absolute path. Example: F:\\pdf\\dest.pdf Wu      * @returnthe prompt information for the success of the operation. If 1 is returned, indicating that the source file could not be found, or if the Url.properties configuration error is returned, if 0 - * indicates successful operation; return 1 indicates conversion failed About      */   $      Public Static intoffice2pdf (String sourcefile, String destfile) { -         Try {   -File Inputfile =NewFile (sourcefile);  -             if(!inputfile.exists ()) {   A                 return-1;//source file not found, return-1 +             }   the    -             //If the destination path does not exist, the path is created $File OutputFile =NewFile (destfile);  the             if(!Outputfile.getparentfile (). exists ()) {   the outputfile.getparentfile (). Mkdirs ();  the             }   the                -             //Connect to a OpenOffice.org instance running on port 8100 inOpenofficeconnection connection =NewSocketopenofficeconnection ( the"127.0.0.1", 8100);  the Connection.connect ();  About    the             //Convert theDocumentconverter converter =NewOpenofficedocumentconverter ( the connection);  + Converter.convert (Inputfile, outputFile);  -    the             //Close the connectionBayi Connection.disconnect ();  the    the             return0;  -}Catch(FileNotFoundException e) { - E.printstacktrace ();  the             return-1;  the}Catch(connectexception e) { the E.printstacktrace ();  the}Catch(IOException e) { - E.printstacktrace ();  the         }   the    the         return1; 94}

Java converts word to PDF with OpenOffice

Related Article

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.