// Classes for bootstrapping connection to OpenOffice.org import com.sun.star.comp.helper.Bootstrap; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.bridge.XUnoUrlResolver; import com.sun.star.frame.XController; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XDispatchHelper; import com.sun.star.frame.XDispatchProvider; import com.sun.star.frame.XFrame; import com.sun.star.frame.XStorable; import com.sun.star.io.IOException; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.lang.XSingleServiceFactory; import com.sun.star.lang.XComponent; import com.sun.star.table.XCell; import com.sun.star.text.XTextDocument; import com.sun.star.text.XText; import com.sun.star.text.XTextCursor; import com.sun.star.text.XTextTable; import com.sun.star.uno.XNamingService; import com.sun.star.uno.XComponentContext; import com.sun.star.uno.UnoRuntime; import com.sun.star.util.XCloseable; import com.sun.star.util.CloseVetoException; import com.sun.star.view.XSelectionSupplier; public class i48642 { // These objects are used for creating and accessing OpenOffice.org API objects private XMultiComponentFactory mxMCF; private XMultiServiceFactory mxMSF; private XComponentContext mxComponentContext; private XComponentLoader mxComponentLoader; // The default connection string used to connect to running OpenOffice.org public static final String DEFAULT_CONNECTION_STRING = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"; public void setupConnection() { try { System.err.println( "Trying to connect to an Office process... "); /* 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. */ mxComponentContext = 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. */ mxMCF = mxComponentContext.getServiceManager(); /* Creates an instance of the component UnoUrlResolver which supports the services specified by the factory. */ Object objectUrlResolver = mxMCF.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", mxComponentContext); // Create a new url resolver XUnoUrlResolver xurlresolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, objectUrlResolver); // Resolves an object that is specified as follow: // uno:;; Object objectInitial = xurlresolver.resolve( DEFAULT_CONNECTION_STRING); // Create a service manager from the initial object mxMCF = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, objectInitial); // Query for the XPropertySet interface. XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, mxMCF); // Get the default context from the office server. Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue( "DefaultContext"); // Query for the interface XComponentContext. mxComponentContext = (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. */ mxComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, mxMCF.createInstanceWithContext( "com.sun.star.frame.Desktop", mxComponentContext)); // Query for an XMultiServiceFactory instance from the global // service manager if (mxMSF == null) { mxMSF = (XMultiServiceFactory)UnoRuntime.queryInterface( XMultiServiceFactory.class, mxComponentContext.getServiceManager()); } } catch(Exception exception) { System.err.println(exception); } } public XTextDocument openWriter() { XTextDocument oDoc = null; XComponent aDoc = null; try { PropertyValue[] loadProps = new PropertyValue[0]; aDoc = mxComponentLoader.loadComponentFromURL( "private:factory/swriter", "_blank", 0, loadProps); oDoc = (XTextDocument) UnoRuntime.queryInterface( XTextDocument.class, aDoc); } catch(Exception e) { System.err.println("Error opening new document" + e); } return oDoc; } public void exportToPDF( XTextDocument xDoc, String sURL ) { PropertyValue[] aExportProperties = new PropertyValue[2]; aExportProperties[0] = new PropertyValue(); aExportProperties[0].Name = "FilterName"; aExportProperties[0].Value = "writer_pdf_Export"; aExportProperties[1] = new PropertyValue(); aExportProperties[1].Name = "Overwrite"; aExportProperties[1].Value = new Boolean(true); XStorable xStore = (XStorable)UnoRuntime.queryInterface( XStorable.class, xDoc); try { xStore.storeToURL(sURL, aExportProperties); } catch( IOException aExcept) { System.err.println("Error storing document"); } } public i48642() { } public static void main(String args[]) { boolean bLock = false; if(args.length > 0) { if(args[0].equals("-lock")) { bLock = true; } } i48642 aTest = new i48642(); aTest.setupConnection(); for( int nDoc = 0; nDoc < 5; ++nDoc) { XTextDocument xTestDoc = aTest.openWriter(); XText xTestText = xTestDoc.getText(); XTextCursor xTestCursor = xTestText.createTextCursor(); try { if(bLock) xTestDoc.lockControllers(); for( int nTable = 0; nTable < 5; ++nTable) { XMultiServiceFactory xDocFactory = (XMultiServiceFactory)UnoRuntime.queryInterface( XMultiServiceFactory.class, xTestDoc); XTextTable xTable = (XTextTable)UnoRuntime.queryInterface( XTextTable.class, xDocFactory.createInstance("com.sun.star.text.TextTable")); xTable.initialize(40,4); xTestText.insertTextContent( xTestCursor, xTable, false); for( int i = 1; i < 41; ++i ) { for( int nLetter = 0; nLetter <4; ++nLetter ) { String sName; switch( nLetter ) { case 0: sName = "A"; break; case 1: sName = "B"; break; case 2: sName = "C"; break; default: sName = "D"; break; } sName += i; XCell xCell = xTable.getCellByName( sName ); XText xCellText = (XText)UnoRuntime.queryInterface( XText.class, xCell ); XTextCursor xCellCursor = xCellText.createTextCursor(); xCellText.setString(sName); } } } if(bLock) xTestDoc.unlockControllers(); } catch(Exception e) { System.err.println("Error modifying document"); if(bLock) xTestDoc.unlockControllers(); } String sURL = "file:///d:/temp/i48642_"; sURL += nDoc; sURL += ".pdf"; aTest.exportToPDF( xTestDoc, sURL ); try { XCloseable xClose = (XCloseable)UnoRuntime.queryInterface(XCloseable.class, xTestDoc); xClose.close(true); } catch(CloseVetoException exception) { System.err.println(exception); } } System.exit(0); } }