Convert PPT and doc into PDF using OpenOffice

Source: Internet
Author: User
Tags create directory

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

Jodconverter jar Pack Download: http://sourceforge.net/projects/jodconverter/files/JODConverter/

PS: When installing OpenOffice, the path as far as possible does not have the Chinese or the space and so on information, will cause the call error.


Package com.kingt;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.net.ConnectException;

Import Java.util.Date;
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 {/** * Converts Office documents to PDF. You need to run this function with OpenOffice, OpenOffice download address is * http://www.openoffic e.org/* @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) {Process pro = null;
		Openofficeconnection connection = null;
			try {file Inputfile = new file (sourcefile);
			if (!inputfile.exists ()) {return-1;//file does not exist}//folder does not exist create directory file OutputFile = new file (destfile);
			if (!outputfile.getparentfile (). exists ()) {Outputfile.getparentfile (). Mkdirs ();
			} String openoffice_home = "F:\\tool\\openoffice4";
			if (Openoffice_home.charat (Openoffice_home.length ()-1)!= ' \ ') {openoffice_home = = "\"; }//Start OpenOffice service String command = openoffice_home + "program\\soffice.exe-headless-accept=\" Socket,host
			=127.0.0.1,port=8100;urp;\ "-nofirststartwizard";
			Pro = Runtime.getruntime (). exec (command); Connect to a OpenOffice.org instance running on port 8100 connection = new Socketopenofficeconnection ("127.0.0.1",
			8100);

			Connection.connect ();
	Convert Documentconverter converter = new Openofficedocumentconverter (				connection);

			Converter.convert (Inputfile, outputfile);
		return 0;
			catch (FileNotFoundException e) {e.printstacktrace ();
		return-1;
		catch (Connectexception e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace ();
			}finally{if (connection!=null) {//Close the connection connection.disconnect ();
			}//Close the OpenOffice service process if (pro!=null) {Pro.destroy ();
	} return 1;
		public static void Main (string[] args) {Date startdate = new Date ();
		String sourcefile = "D:\\code\\alipay.ppt";
		String destfile = "D:\\code\\change2.pdf";
		System.out.println (Office2pdf.office2pdf (sourcefile, destfile));
		Date EndDate = new Date ();
	System.out.println ("The cost time is" + (Enddate.gettime ()-startdate.gettime ());
 }
}

The command to execute the service is called directly in the Java code, and then the process is killed directly when the transcoding is complete.

There is a problem: when the first execution will be an error,

Java.net.ConnectException:connection Failed:socket,host=localhost,port=8080,tcpnodelay=1: Java.net.ConnectException:Connection Refused:connect
Once again the success of the operation, as for the problem can not find a solution.



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.