Home| New Wiki | | Login | User registry | Home Tree PDF
OpenOffice ONE
Owner:, Version: 6, Date:Tue 30, January 2007,

StartOffice (Open Office) provee un API de comunicaciones que permite que programas externos puedan comunicarse con cualquiera de sus aplicaciones.

Este ejemplo carga un archivo write y lo escribe como PDF.

Listado 1: FirstConnection.java

  import com.sun.star.bridge.*;
  import com.sun.star.uno.*;
  import com.sun.star.lang.*;
  import com.sun.star.frame.*;
  import com.sun.star.beans.*;
  import com.sun.star.util.*;
  
public class FirstConnection {
 private XComponentContext xRemoteContext = null;
 private XMultiComponentFactory xRemoteServiceManager = null;

 public static void main(String[] args) {
   FirstConnection firstConnection1 = new FirstConnection();
   try {
     firstConnection1.useConnection();
   } catch (java.lang.Exception e) {
     e.printStackTrace();
   } finally {
     System.exit(0);
   }
 }

 protected void useConnection() throws java.lang.Exception {
   try {
     xRemoteServiceManager =
       this.getRemoteServiceManager(
         "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
     String available =
       (null != xRemoteServiceManager ? "available" : "not available");
     System.out.println("remote ServiceManager is " + available);
     //
     // do something with the service manager...
     //

     // get the Desktop, we need its XComponentLoader interface to load a new document
     Object desktop =
       xRemoteServiceManager.createInstanceWithContext(
         "com.sun.star.frame.Desktop",
         xRemoteContext);

     // query the XComponentLoader interface from the desktop
     XComponentLoader xComponentLoader =
       (XComponentLoader) UnoRuntime.queryInterface(
         XComponentLoader.class,
         desktop);
     // create empty array of PropertyValue structs, needed for loadComponentFromURL
     PropertyValue[] loadProps = new PropertyValue[0];

     XComponent xComponent =
       xComponentLoader.loadComponentFromURL(
         "file:///c:/test.sxw",
         "_blank",
         0,
         loadProps);
     XStorable xStorable =
       (XStorable) UnoRuntime.queryInterface(
         XStorable.class,
         xComponent);
     System.out.println("Archivo cargado=" + xStorable.getLocation());

     PropertyValue[] lProperties = new PropertyValue[2];
     lProperties[0] = new com.sun.star.beans.PropertyValue();
     lProperties[0].Name = "FilterName";
     lProperties[0].Value = "writer_pdf_Export";
     // writer_pdf_Export, draw_pdf_Export
     lProperties[1] = new com.sun.star.beans.PropertyValue();
     lProperties[1].Name = "Overwrite ";
     lProperties[1].Value = new Boolean(true);
     String url = "file:///c:/result.pdf";

     xStorable.storeToURL(url, lProperties);
     System.out.println("Archivo escrito=" + url);
     XCloseable xCloseable =
       (XCloseable) UnoRuntime.queryInterface(
         XCloseable.class,
         xComponent);
     if (xCloseable != null) {
       System.out.println("Using XCloseable::close on component");
       xCloseable.close(true);
     } else {
       System.out.println("Using XComponent::dispose on component");
       xComponent.dispose();
     }
     XDesktop xDesktop =
       (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
     xDesktop.terminate();
     System.out.println("Using XComponent::dispose on desktop");
   } catch (com.sun.star.connection.NoConnectException e) {
     System.err.println("No process listening on the resource");
     e.printStackTrace();
     throw e;
   }
 }

 protected XMultiComponentFactory getRemoteServiceManager(String unoUrl)
   throws java.lang.Exception {
   if (xRemoteContext == null) {
     // First step: create local component context, get local servicemanager and
     // ask it to create a UnoUrlResolver object with an XUnoUrlResolver interface
     XComponentContext xLocalContext =
       com
         .sun
         .star
         .comp
         .helper
         .Bootstrap
         .createInitialComponentContext(
         null);
     XMultiComponentFactory xLocalServiceManager =
       xLocalContext.getServiceManager();
     Object urlResolver =
       xLocalServiceManager.createInstanceWithContext(
         "com.sun.star.bridge.UnoUrlResolver",
         xLocalContext);
     // query XUnoUrlResolver interface from urlResolver object
     XUnoUrlResolver xUnoUrlResolver =
       (XUnoUrlResolver) UnoRuntime.queryInterface(
         XUnoUrlResolver.class,
         urlResolver);
     // Second step: use xUrlResolver interface to import the remote StarOffice.ServiceManager,
     // retrieve its property DefaultContext and get the remote servicemanager
     Object initialObject = xUnoUrlResolver.resolve(unoUrl);
     XPropertySet xPropertySet =
       (XPropertySet) UnoRuntime.queryInterface(
         XPropertySet.class,
         initialObject);
     Object context = xPropertySet.getPropertyValue("DefaultContext");
     xRemoteContext =
       (XComponentContext) UnoRuntime.queryInterface(
         XComponentContext.class,
         context);
   }
   return xRemoteContext.getServiceManager();
 }
}

Compilacion

@echo off
set UNO="C:Archivos de programaOpenOffice.org1.1.0\program\classes\"
set CLASSPATH=".;%UNO%\aportisdoc.jar;%UNO%\jut.jar;%UNO%\common.jar;
  %UNO%\ridl.jar;%UNO%\java_uno.jar;%UNO%\XSLTValidate.jar;
  %UNO%\htmlsoff.jar;%UNO%\XFlatXml.jar;%UNO%\XSLTFilter.jar;
  %UNO%\XMergeBridge.jar;%UNO%\officebean.jar;%UNO%\docbook.jar;
  %UNO%\juh.jar;%UNO%\sandbox.jar;%UNO%\classes.jar;%UNO%\report.jar;
  %UNO%\xmerge.jar;%UNO%\jurt.jar;%UNO%\java_uno_accessbridge.jar;
  %UNO%\xt.jar;%UNO%\unoil.jar"
javac *.java

Ejecucion invisible de OpenOffice

Se puede ejecutar OpenOffice en formato invisible:

"C:\Archivos de programa\OpenOffice.org1.1.0\program\soffice.exe" 
   "-invisible" 
   "-writer" 
   "-accept=socket,port=8100;urp;"

Ejecucion del comando java

@echo off
set UNO="C:\Archivos de programa\OpenOffice.org1.1.0\program\classes\"
set CLASSPATH=".;%UNO%\aportisdoc.jar;%UNO%\jut.jar;%UNO%\common.jar;
  %UNO%\ridl.jar;%UNO%\java_uno.jar;%UNO%\XSLTValidate.jar;
  %UNO%\htmlsoff.jar;%UNO%\XFlatXml.jar;%UNO%\XSLTFilter.jar;
  %UNO%\XMergeBridge.jar;%UNO%\officebean.jar;%UNO%\docbook.jar;
  %UNO%\juh.jar;%UNO%\sandbox.jar;%UNO%\classes.jar;%UNO%\report.jar;
  %UNO%\xmerge.jar;%UNO%\jurt.jar;%UNO%\java_uno_accessbridge.jar;
  %UNO%\xt.jar;%UNO%\unoil.jar"
java FirstConnection


Edit - History - Extract PDF - Extract Tree as PDF

Last Modified

Thu, Nov 19 Sat, Oct 3 Fri, Sep 25 Mon, Aug 3 Mon, Apr 27 Sat, Mar 28 Mon, Jan 19 Tue, Jan 6

Home| New Wiki