We need open office s/w and it has to be installed.
To execute the java program few jar files are needed. they are,
juh.jar
jurt.jar
jut.jar
ridl.jar
sandbox.jar
unoil.jar
To start openoffice server ,the following class has to be used.
import java.net.*;
/**
* Static 'Helper' class - OpenOffice Server Utilities.
*/
public class OpenOfficeServerUtility {
public static boolean checkServerAvailability(String host,int port) {
try {
Socket testSocket = new Socket(host,port);
return(true);
}
catch(Exception e) {
return(false);
}
}
public static void runOpenOfficeServer(String serverExecutable,String host,int port,long delay,boolean runInvisibly) throws Exception {
String execString = serverExecutable;
if(runInvisibly) {
execString = execString + " -invisible";
}
execString = execString + " -accept=socket,host=" + host + ",port=" + port + ";urp;StarOffice.ServiceManager";
System.out.println(execString);
Process p = Runtime.getRuntime().exec(execString);
/*Wait for OpenOffice server to sort its life out. Kludgy, but cant find a better way to do this,
as the process doesn't write anything to it output stream*/
Thread.sleep(delay);
}
}
To convert the files following java code shall be used.
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.bridge.XUnoUrlResolver;
import org.sockdrawer.util.openOffice.OpenOfficeServerUtility;
/** The class <CODE>DocumentConverter</CODE> allows you to convert all documents in
* a given directory and in its subdirectories to a given type. A converted
* document will be created in the same directory as the origin document.
*
*/
public class DocumentConverter {
/** Containing the loaded documents
*/
static XComponentLoader xcomponentloader = null;
/** Containing the given type to convert to
*/
static String stringConvertType = "";
/** Containing the given extension
*/
static String stringExtension = "";
/** Containing the current file or directory
*/
static String indent = "";
/** Traversing the given directory recursively and converting their files to the
* favoured type if possible
*/
static void traverse() {
// Converting the document to the favoured type
try {
// Composing the URL by replacing all backslashs
String stringUrl = "file:///D:/Raghuram/doctohtml/Advisor_08_04_2010_03_12_PM.html";
// Loading the wanted document
PropertyValue[] openpropertyvalue = new PropertyValue[1];
openpropertyvalue[ 0 ] = new PropertyValue();
openpropertyvalue[ 0 ].Name = "Hidden";
openpropertyvalue[ 0 ].Value = new Boolean(true);
Object objectDocumentToStore = DocumentConverter.xcomponentloader.loadComponentFromURL( stringUrl, "_blank", 0, openpropertyvalue );
Thread.sleep(5000);
// Getting an object that will offer a simple way to store a document to a URL.
XStorable xstorable = ( XStorable ) UnoRuntime.queryInterface( XStorable.class, objectDocumentToStore );
// Preparing properties for converting the document
PropertyValue propertyvalue[] = new PropertyValue[ 1 ];
// Setting the flag for overwriting
propertyvalue[ 0 ] = new PropertyValue();
propertyvalue[ 0 ].Name = "FilterName";
//for pdf
propertyvalue[ 0 ].Value = "writer_pdf_Export";
//for rtf
//propertyvalue[ 0 ].Value="Rich Text Format";
System.out.println("before storing... \n");
// Storing and converting the document
//for pdf
xstorable.storeToURL("file:///D:/Raghuram/doctohtml/testpdf.pdf", propertyvalue);
//for rtf
// xstorable.storeToURL("file:///D:/Raghuram/doctohtml/testpdf.pdf", propertyvalue);
System.out.println("after storing... \n");
// Getting the method dispose() for closing the document
XComponent xcomponent =
( XComponent ) UnoRuntime.queryInterface( XComponent.class,
xstorable );
// Closing the converted document
xcomponent.dispose();
System.out.println("End !!! \n");
}
catch( Exception exception ) {
exception.printStackTrace();
}
}
/** Connecting to the office with the component UnoUrlResolver and calling the
* static method traverse
* @param args The array of the type String contains the directory, in which all files should be
* converted, the favoured converting type and the wanted extension
*/
public static void main( String args[] ) {
try {
String openOfficeServerHost = "localhost";
int openOfficeServerPort = 8100;
if(!OpenOfficeServerUtility.checkServerAvailability(openOfficeServerHost,openOfficeServerPort)) {
try {
// String Officelocation = commconfig().getProperty("soffice");
/*
* Try to startup the OpenOffice server as a separate process.
* The '20000' value is simple delay to give the server time to startup beacuse,
* unfortunately, the startup process doesn't seem to return anything.
* Kludgy :(
*/
OpenOfficeServerUtility.runOpenOfficeServer("soffice",openOfficeServerHost,openOfficeServerPort,20000,false);
}
catch(Exception e) {
System.out.println("Unable to start the OpenOffice server");
e.printStackTrace();
//System.exit(1);
}
}
String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";
/* Bootstraps a component context with the jurt base components
registered. Component context to be granted to a component for running.
Arbitrary values can be retrieved from the context. */
XComponentContext xComponentContext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
/* Gets the service manager instance to be used (or null). This method has
been added for convenience, because the service manager is a often used
object. */
XMultiComponentFactory xMultiComponentFactory = xComponentContext.getServiceManager();
/* Creates an instance of the component UnoUrlResolver which
supports the services specified by the factory. */
Object objectUrlResolver = xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xComponentContext );
// Create a new url resolver
XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class,
objectUrlResolver );
// Resolves an object that is specified as follow:
// uno:<connection description>;<protocol description>;<initial object name>
Object objectInitial = xurlresolver.resolve( sConnectionString );
// Create a service manager from the initial object
xMultiComponentFactory = ( XMultiComponentFactory )
UnoRuntime.queryInterface( XMultiComponentFactory.class, objectInitial );
// Query for the XPropertySet interface.
XPropertySet xpropertysetMultiComponentFactory = ( XPropertySet )
UnoRuntime.queryInterface( XPropertySet.class, xMultiComponentFactory );
// Get the default context from the office server.
Object objectDefaultContext =
xpropertysetMultiComponentFactory.getPropertyValue( "DefaultContext" );
// Query for the interface XComponentContext.
xComponentContext = ( XComponentContext ) UnoRuntime.queryInterface(
XComponentContext.class, objectDefaultContext );
/* A desktop environment contains tasks with one or more
frames in which components can be loaded. Desktop is the
environment for components which can instanciate within
frames. */
xcomponentloader = ( XComponentLoader )
UnoRuntime.queryInterface( XComponentLoader.class,
xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", xComponentContext ) );
// Starting the conversion of documents in the given directory and subdirectories
traverse();
System.exit(0);
}
catch( Exception exception ) {
System.err.println( exception );
}
}
}
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.bridge.XUnoUrlResolver;
import org.sockdrawer.util.openOffice.OpenOfficeServerUtility;
/** The class <CODE>DocumentConverter</CODE> allows you to convert all documents in
* a given directory and in its subdirectories to a given type. A converted
* document will be created in the same directory as the origin document.
*
*/
public class DocumentConverter {
/** Containing the loaded documents
*/
static XComponentLoader xcomponentloader = null;
/** Containing the given type to convert to
*/
static String stringConvertType = "";
/** Containing the given extension
*/
static String stringExtension = "";
/** Containing the current file or directory
*/
static String indent = "";
/** Traversing the given directory recursively and converting their files to the
* favoured type if possible
*/
static void traverse() {
// Converting the document to the favoured type
try {
// Composing the URL by replacing all backslashs
String stringUrl = "file:///D:/Raghuram/doctohtml/Advisor_08_04_2010_03_12_PM.html";
// Loading the wanted document
PropertyValue[] openpropertyvalue = new PropertyValue[1];
openpropertyvalue[ 0 ] = new PropertyValue();
openpropertyvalue[ 0 ].Name = "Hidden";
openpropertyvalue[ 0 ].Value = new Boolean(true);
Object objectDocumentToStore = DocumentConverter.xcomponentloader.loadComponentFromURL( stringUrl, "_blank", 0, openpropertyvalue );
Thread.sleep(5000);
// Getting an object that will offer a simple way to store a document to a URL.
XStorable xstorable = ( XStorable ) UnoRuntime.queryInterface( XStorable.class, objectDocumentToStore );
// Preparing properties for converting the document
PropertyValue propertyvalue[] = new PropertyValue[ 1 ];
// Setting the flag for overwriting
propertyvalue[ 0 ] = new PropertyValue();
propertyvalue[ 0 ].Name = "FilterName";
//for pdf
propertyvalue[ 0 ].Value = "writer_pdf_Export";
//for rtf
//propertyvalue[ 0 ].Value="Rich Text Format";
System.out.println("before storing... \n");
// Storing and converting the document
//for pdf
xstorable.storeToURL("file:///D:/Raghuram/doctohtml/testpdf.pdf", propertyvalue);
//for rtf
// xstorable.storeToURL("file:///D:/Raghuram/doctohtml/testpdf.pdf", propertyvalue);
System.out.println("after storing... \n");
// Getting the method dispose() for closing the document
XComponent xcomponent =
( XComponent ) UnoRuntime.queryInterface( XComponent.class,
xstorable );
// Closing the converted document
xcomponent.dispose();
System.out.println("End !!! \n");
}
catch( Exception exception ) {
exception.printStackTrace();
}
}
/** Connecting to the office with the component UnoUrlResolver and calling the
* static method traverse
* @param args The array of the type String contains the directory, in which all files should be
* converted, the favoured converting type and the wanted extension
*/
public static void main( String args[] ) {
try {
String openOfficeServerHost = "localhost";
int openOfficeServerPort = 8100;
if(!OpenOfficeServerUtility.checkServerAvailability(openOfficeServerHost,openOfficeServerPort)) {
try {
// String Officelocation = commconfig().getProperty("soffice");
/*
* Try to startup the OpenOffice server as a separate process.
* The '20000' value is simple delay to give the server time to startup beacuse,
* unfortunately, the startup process doesn't seem to return anything.
* Kludgy :(
*/
OpenOfficeServerUtility.runOpenOfficeServer("soffice",openOfficeServerHost,openOfficeServerPort,20000,false);
}
catch(Exception e) {
System.out.println("Unable to start the OpenOffice server");
e.printStackTrace();
//System.exit(1);
}
}
String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";
/* Bootstraps a component context with the jurt base components
registered. Component context to be granted to a component for running.
Arbitrary values can be retrieved from the context. */
XComponentContext xComponentContext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
/* Gets the service manager instance to be used (or null). This method has
been added for convenience, because the service manager is a often used
object. */
XMultiComponentFactory xMultiComponentFactory = xComponentContext.getServiceManager();
/* Creates an instance of the component UnoUrlResolver which
supports the services specified by the factory. */
Object objectUrlResolver = xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xComponentContext );
// Create a new url resolver
XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class,
objectUrlResolver );
// Resolves an object that is specified as follow:
// uno:<connection description>;<protocol description>;<initial object name>
Object objectInitial = xurlresolver.resolve( sConnectionString );
// Create a service manager from the initial object
xMultiComponentFactory = ( XMultiComponentFactory )
UnoRuntime.queryInterface( XMultiComponentFactory.class, objectInitial );
// Query for the XPropertySet interface.
XPropertySet xpropertysetMultiComponentFactory = ( XPropertySet )
UnoRuntime.queryInterface( XPropertySet.class, xMultiComponentFactory );
// Get the default context from the office server.
Object objectDefaultContext =
xpropertysetMultiComponentFactory.getPropertyValue( "DefaultContext" );
// Query for the interface XComponentContext.
xComponentContext = ( XComponentContext ) UnoRuntime.queryInterface(
XComponentContext.class, objectDefaultContext );
/* A desktop environment contains tasks with one or more
frames in which components can be loaded. Desktop is the
environment for components which can instanciate within
frames. */
xcomponentloader = ( XComponentLoader )
UnoRuntime.queryInterface( XComponentLoader.class,
xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", xComponentContext ) );
// Starting the conversion of documents in the given directory and subdirectories
traverse();
System.exit(0);
}
catch( Exception exception ) {
System.err.println( exception );
}
}
}
There are other types of conversion as well. i have given a link below for your reference.
http://www.oooforum.org/forum/viewtopic.phtml?t=23843
We can execute the code in linux as well. But the following line has to changed.
OpenOfficeServerUtility.runOpenOfficeServer("soffice",openOfficeServerHost,openOfficeServerPort,20000,false);
OpenOfficeServerUtility.runOpenOfficeServer("/opt/openoffice.org3/program/soffice.bin",openOfficeServerHost,openOfficeServerPort,20000,false);
No comments:
Post a Comment