Openoffice UNO API
Openoffice UNO API
lot.
The
jars juh.jar,
available
at openoffice.org/basis3.0/program/classes.
You also need the program directory on your class path ( eg: /opt/openoffice.org3/program ), otherwise youll get
the following error message: com.sun.star.comp.helper.BootstrapException: no office executable found!
First thing to do is to connect to the OpenOffice. They following code will start an instance of OpenOffice.
// Get the remote office component context
XComponentContext xContext = Bootstrap.bootstrap();
// Get the remote office service manager
XMultiComponentFactory xMCF = xContext.getServiceManager();
// Get the root frame (i.e. desktop) of openoffice framework.
Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
// Desktop has 3 interfaces. The XComponentLoader interface provides ability to load components.
XComponentLoader xCLoader = ( XComponentLoader ) UnoRuntime.queryInterface(XComponentLoader.class,
oDesktop);
Paper Formating
The default paper format and orientation is A4 and portrait. To change paper orientation
XPrintable xPrintable = ( XPrintable ) UnoRuntime.queryInterface(XPrintable.class, document);
PropertyValue[] printerDesc = new PropertyValue[2];
// Paper Orientation
printerDesc[0] = new PropertyValue();
printerDesc[0].Name = "PaperOrientation";
printerDesc[0].Value = PaperOrientation.LANDSCAPE;
// Paper Format
printerDesc[1] = new PropertyValue();
printerDesc[1].Name = "PaperFormat";
printerDesc[1].Value = PaperFormat.A3;
xPrintable.setPrinter(printerDesc);
When you want to export it to PDF, you need to specify a filter name: writer_pdf_Export. Also note that you need
to
call
storeToUrl
instead
of
storeAsUrl, otherwise
youll
get the
following
exception:
com.sun.star.task.ErrorCodeIOException.
// export document to pdf
storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "writer_pdf_Export";
xStorable.storeToURL(file:///tmp/OOo_doc.pdf", storeProps);
We can also save the document in other formats by specifying the filter name. For example, to save to rich text
format
// the url where the document is to be saved
String storeUrl = file:///tmp/OOo_doc.rtf ";
// Save the document
XStorable xStorable = ( XStorable )UnoRuntime.queryInterface(XStorable.class, document);