Java uses OpenOffice to convert Word to Pdf__java

Source: Internet
Author: User

This article on the basis of the original text to be modified, please refer to the original:

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. Software that needs to be used

OpenOffice Download Address http://www.openoffice.org/

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


2. Start the OpenOffice service

Install the OpenOffice after installing 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 is 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 a Jodconverter-related jar package to your project


4. The following is the implementation code

The attachment contains an out-of-the-box example of a project that you can use to directly import Eclipse to run

/** * Converts Office documents to PDF.  OpenOffice is required to run this function, OpenOffice download address is * 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. Can be office2003-2007 in all formats of the document, Office2010 not tested. Includes. doc, *. docx,. xls,. xlsx, ppt,. pptx. Example: F:\\office\\source.doc * @param destfile * target file. Absolute path. Example: F:\\pdf\\dest.pdf * @return Prompt for successful operation. If 1 is returned, the source file cannot be found, or the url.properties configuration error; If 0 is returned, * indicates successful operation; Returns 1, which means the conversion failed/public static int office2pdf (string sourcefile, String destfile) {try {file Inputfile = new File
			(sourcefile); if (!inputfile.exists ()) {return-1;//cannot find the source file, return-1}//If the destination path does not exist, create the new path file outputfile = new file (dest
			File);
			if (!outputfile.getparentfile (). exists ()) {Outputfile.getparentfile (). Mkdirs (); }//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 ();
		return 0;
			catch (FileNotFoundException e) {e.printstacktrace ();
		return-1;
		catch (Connectexception e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace ();
	return 1; }


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.