Convert PDF via OpenOffice

Source: Internet
Author: User
Package OpenOffice;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;

Import java.net.ConnectException;
Import Com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
Import Com.artofsolving.jodconverter.DocumentConverter;
Import Com.artofsolving.jodconverter.DocumentFormat;
Import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
Import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;


Import Com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
 
	public class Wordtopdf {public static int default_port = 8100;
	
	public static String Default_host = "127.0.0.1";  /** * Before executing, please start OpenOffice service * Enter $oo_home\program under * Execute soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;" -nofirststartwizard * @param xlsfile * @param targetfile * @throws Exception/public static void SaveAs (STR INg Xlsfile, String targetfile) throws Exception {file XLSF = new file (xlsfile);
		File TARGETF = new file (targetfile);
		Get file format defaultdocumentformatregistry Formatreg = new Defaultdocumentformatregistry ();
		Documentformat Pdfformat = formatreg.getformatbyfileextension ("PDF");
		Documentformat Docformat = formatreg.getformatbyfileextension ("Doc");
		The form of stream stream InputStream InputStream = new FileInputStream (XLSF);
 
		OutputStream outputstream = new FileOutputStream (TARGETF);
		/** * * */openofficeconnection connection = new socketopenofficeconnection (8100);
		System.out.println (connection);
			try {connection.connect ();
 
			Documentconverter converter = new Openofficedocumentconverter (connection);
			SYSTEM.OUT.PRINTLN ("inputstream------" + inputstream);
			SYSTEM.OUT.PRINTLN ("outputstream------" + outputstream);
		Converter.convert (InputStream, Docformat, OutputStream, Pdfformat);
		catch (Connectexception e) {e.printstacktrace ();
		finally {	if (connection!= null) {connection.disconnect ();
			connection = null;
}} public static void Main (string[] args) {System.out.println ("Begin ...");
String path1 = "D:/temp/111.doc";
		String path2 = "D:/temp/111.pdf";
		try {SaveAs (path1, path2);
		catch (Exception e) {e.printstacktrace ();
	} System.out.println ("OK!!!");
 }
	
}

Linux boot OpenOffice Service:

First, start Office conversion to PDF Server "OpenOffice Server"
1, into the OpenOffice installation directory
Installation path for CD Opeonofiice/program
2, boot port monitoring
soffice-headless-accept= "Socket,host=127.0.0.1,port=8100;urp"-nofirststartwizard &
3, to see if the success of the launch, there are 8100 ports that start successfully
Netstat-an
As shown below:
[Root@localhost program]# Netstat-an|more
Active Internet connections (servers and established)
Proto recv-q Send-q Local address Foreign address Stat
E
TCP 0 0 127.0.0.1:2208 0.0.0.0:* LIST
En
TCP 0 0 127.0.0.1:8100 0.0.0.0:* LIST
En

Linux suspend OpenOffice Service:

Enter command line Ps-a
OpenOffice Process ID found
Then enter kill [process ID]

Jodconverter API:
http://jodconverter.sourceforge.net/api/

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.