OpenOfficeOpenOffice.org examples


Contents

Overview
Additional Resources
Java examples
C++ examples
OpenOffice.org Basic examples
Object Linking and Embedding (OLE) examples

Overview

The OpenOffice.org software is a complete, feature-rich office productivity product, that provides in combination with the OpenOffice.org Developer Kit (ODK) all the essential tools for building and deploying custom solutions to build on, or integrate with, the OpenOffice.org suite of products.

The ODK is a set of tools, libraries, jar files, header files and idl files which are necessary to develop components for the OpenOffice.org using the OpenOffice API and the OpenOffice.org component technology UNO (Universal Network Objects). Furthermore, the tarballs contain all below mentioned examples (C++, Java, and Basic), which demonstrate the UNO technology and the use of the OpenOffice.org API.

Further on, the ODK is the basic stuff for external developers to develop components in any language for which a binding exists. Currently there are language bindings for OpenOffice.org Basic, Java and different C++ compilers on different platforms for OpenOffice.org without using OpenOffice.org build environment, thus covering the two most commonly used programming languages. There will be no limit for office automation in heterogeneous environments. Currently supported platforms of the ODK are Linux, Solaris (sparc) and Windows.

The OpenOffice.org API defines the interface for accessing office functionality from different programming languages. The OpenOffice API reference manual is available here.

The following examples demonstrate how to benefit from the included word processor, spreadsheet, presentation software, graphics program, and database software.

Additional Resources

Example Description
Programmer's Tutorial This PDF document presents the basic concepts of the OpenOffice API, the document models (including some UML diagrams) and a bunch of examples in StarBASIC as well. (Version for StarOffice 5.2)
UNO Development Kit Homepage UNO (Universal Network Objects) is an interface based object model. UNO is a specification for a communication model among distributed objects. It allows these objects to be implemented in any language for which a binding exists. Currently there are language bindings for Java and different C++ compilers on different platforms.
OpenOffice API Homepage The OpenOffice.org API is based on the OpenOffice.org component technology and consists of a wide range of interfaces defined in a CORBA-like IDL. While the component technology determines how the components or applications communicate with each other, the OpenOffice.org API defines the interface for accessing office functionality from different programming languages. The OpenOffice API reference manual is available from the homepage.
Developing components with UNO The tutorial "Developing components with UNO" explains the fundamental concepts (ODK, UNO), shows how to access existing services in UNO, and how to write simple UNO components. The text is enhanced with Java and C++ code.
NetBeans If you need an integrated development environment (IDE) you should take NetBeans into account. NetBeans is an open source, modular IDE, written in the Java programming language.
Guideline for designing OpenOffice APIs The guideline for designing OpenOffice APIs describes general design rules and the definition of API elements.
Office Development Kit (ODK) The ODK is a set of tools, libraries, jar files, header files and idl files which are necessary to develop components for the OpenOffice.org using the OpenOffice API and the OpenOffice.org component technology UNO (Universal Network Objects).

Java examples

In order to connect the following client programs to the running office server, before running those programs, you should invoke the office with the following command:

soffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"

You could also customize the mentioned host and port to your needs.

Example Description
CalcAddins This component adds new functions to the spreadsheet application. After registering the component, the inserted functions can be found with help of the functions autopilot. The new functions are assigned to the category Add-in.
Car The component Car is a very simple, but complete component, which simulates the driving, acceleration, and braking of a car. Moreover, the component demonstrates the use of properties and their listeners.
ConverterServlet This servlet shows you, how to convert arbitrary documents on a remote running office server. The converted document will be pushed from the web server to the client browser.
DocumentConverter Here, the program offers a service, which converts arbitrary documents to a favored document type.
DocumentLoader The DocumentLoader can open a new or an existing document.
DocumentPrinter The DocumentPrinter allows you to print your favored pages of a specified document on your favored printer.
DocumentSaver The DocumentSaver shows you, how to save a document. Furthermore, you can change the type of your document.
GraphicsInserter The GraphicsInserter creates a graphics object on an empty text document by setting its position, width, height, and URL.
Inspector The Instance Inspector is primarily an auxiliary tool for developer, which can present information about an object of the Star Office API at run-time. Dependent on the object to be inspected appropriate methods, interfaces, services, and attributes can be displayed to the developer. The methods, interfaces, services and attributes are represented in a predictable tree. If the attributes contained in an object are to be examined, the appropriate nodes in the tree can be expanded with the help of the mouse. For each non primitive object five categories are offered to the user, as far as those are available with the object concerned.
MinimalComponent This class provides a minimal UNO component. You can take this class as base for your own components. By extensions of this class it is possible to extend also the function range of the Office.
NotesAccess This class creates a spreadsheet document and fills it with existing values of documents from a Lotus Notes database.
ToDo For every to-do listed in a spreadsheet document this component calculates the start date, day of week of the start date, the end date and the day of week of the end date. All calculations are dependent on the values of "Needed Days", "Due Date" and "Status". The columns "Needed Days" and "Status" are mandatory. The first to-do should be placed in row nine. The date to start the calculation should be placed in cell C6. The private holidays should be placed in cell K4/K5 and below.
All rows will be calculated up to the first empty cell in the first column. If a cell in the column "Due Date" will be colored red, you should take a look at your entries.
WriterSelector This class gives you information on the selected objects (text range, text frame, or graphics) at an OpenOffice.org Server.
ChartTypeChange This class loads an OpenOffice.org Calc document and changes the type of the embedded chart.
EuroAdaption The application connects to the office server and gets the multi service factory, opens an empty Calc document, enters an example text, sets the number format to DM, changes the number format to EUR (Euro), and uses the DM/EUR factor on each cell with a content.
SCalc The program connects to the office server and gets the multi service factory. Then an empty calc document will be opened, cell styles will be created, some data will be inserted into the sheets, and the created cell styles will be applied. Finally, a 3D chart will be inserted.
SDraw This program connects to the office server and gets the multi service factory. Afterwards, an empty text document will be created and some shapes will be inserted on the draw page
BookmarkInsertion This application connects to the office server and gets the multi service factory. An empty text document will be opened and an example text will be entered. After all some bookmarks will be inserted.
HardFormatting This program connects to the office server and gets the multi service factory. Furthermore, an empty text document will be opened, an example text will be entered, some text attributes will be inspected, and the PropertyState will be checked from the selection.
SWriter The program connects to the office server and gets the multi service factory. An empty text document will be created, some text will be entered, and a text table, a colored text, and text frame will be inserted.
StyleCreation The example connects to the office server and gets the multi service factory. An empty text document will be opened, a new paragraph style will be created, and applied.
StyleInitialization The program connects to the office server and gets the multi service factory. Thereafter, an empty text document will be opened and an example text will be entered. The paragraph collection will be used, in order to apply a different paragraph style on the paragraphs.
TextDocumentStructure The application connects to the office server and gets the multi service factory. An empty text document will be opened, an enumeration of all paragraphs and an enumeration of all text portions will be created.
TextReplace The example connects to the office server and gets the multi service factory. An empty text document will be created, an example text will be inserted, and some English spelled words will be replaced with US spelled words.
DemoComponent This Java program is a demo component, which exports two services.

Additional informations:

How to write a UNO component in Java
Java in UNO and OpenOffice.org
Debugging Java components
Office and NetBeans

C++ examples

Example Description
Office client This component provides a UNO demo component which demonstrates the communication with a local office. The component will create different document types and close them.
Counter This demo shows how to implement a very simple UNO component and how to access the UNO component from an executable.
Remote client The uno.exe program is used to export an arbitrary service to other processes. In this
example the com.sun.star.uno.Pipe service (which is built in io-module) is exported. The client component implements the com.sun.star.lang.XMain interface. In the run method, it connects to the servers process and retrieves an instance and does some calls on the instance.
Car The component Car is a very simple, but complete component, which simulates the driving, acceleration, and braking of a car. Moreover, the component demonstrates the use of properties and their listeners.

Additional informations:

Writing a simple UNO component in C++
A guide to C++ UNO
C++ UNO Reference

OpenOffice.org Basic examples

The example programs are embedded in StarOffice/OpenOffice.org documents in the samples directory.
To view or edit the StarOffice/OpenOffice.org Basic source, select the appropriate document and then select Tools->Macro.

Most of the examples are stand-alone, but two of them (Creating an Index and Import/Export of ASCII Files) have an associated data file identified by a .txt file name suffix).

These examples are taken from the Programmer's Tutorial. Some of them were ported to Java and you find them above.

These examples are presented in the order in which they appear in Chapter 5.

Example Description
Changing Appearance To change the style used for certain words, you can start with the following example. This code searches for the regular expression the[a-z] which stands for the text portion the followed by exactly one lowercase letter. All occurrences of these four letters are then changed to be displayed in bold characters. The same happens in the next part of the program, this time changing the appearance of all[a-z] to italic. In order for this example to work, you must execute it from an open text document.
Replacing Text

If you regularly receive documents from other people for editing, you might want to make sure that certain words are always written the same. The next example
illustrates this by forcing certain words to be spelled in American English.
In order for this example to work, you must execute it from an open text document. For a real world application, you’d probably want to read the words from an external file.

Using Regular Expressions

Another application of automatic text modification is related to stylistic questions. Suppose your company’s policy is to avoid the use of certain words. You want to replace these words, but you can’t do that automatically, because you have to find the appropriate replacement which depends on the context. So instead of deleting or replacing the offending words automatically, you change their color to make them stand out during a subsequent manual review process.
The following example handles two kinds of unwanted wordings: those which are absolutely forbidden and must be replaced by something else, and those which are considered bad style. A subroutine is responsible for the changes. It can be used to make all words in a list appear in a certain color in the text document. To keep the lists short, we are using regular expressions here which provide for the variants of the words (plural, adjective etc.).

Inserting Bookmarks

The next example does something very similar. This time, however, we do not
change the color of the words but insert a bookmark at each of them. You can thus use the StarOffice navigator to jump directly from word to word. Bookmarks have first to be created using createInstance(). They are then inserted with insertTextContent() at the current text range.

The main difference to the preceding example is the For loop in markList(). Instead of changing the color of the current word, it creates a new bookmark, oBookmark, whose name is the current word with an integer appended. It then
inserts this bookmark at the word.

Creating an Index Indices for text documents can be created manually in StarWriter by clicking on the words that shall appear in the index. If the document is large or if you have to generate indices for several documents, this task should be automated.
Adapting to Euroland Most of the member of the European Union will abandon their old currency in favor of the new Euro in 2001. This requires modifications to all programs using the old currencies. Since the exchange rates for the old currencies have been fixed at the end of 1999, one can already convert old data. The following program does this for all values in a table that are formatted with the currency string DM.
Import/Export of ASCII Files You can use the drawing facilities of StarOffice API to generate a picture from ASCII input. One application would be a hierarchical representation of a directory listing, based on textual data.
Stock Quotes Updater If you want to display stock charts for certain companies, you can fire up your browser every day, go to Yahoo, look up the quote and copy it by hand into a table. Or you can use a program that does all this automatically. The following example relies on the sheet module. It uses URLs to obtain the current stock quotes. The quotes are displayed in sheets, one for each company. We show a line diagram and the numerical values for this company on every sheet. The functionality is hidden in the three subroutines GetValue(), UpdateValue(), and UpdateChart().
Forms and Controls The example document Burger Factory offers you some forms and controls, which you can use to order your desired burger type, beverage, topping, and sauce. By hitting the button "Order now !", your order will be placed.

Additional informations:

Programmer's Tutorial

OLE examples

Example Description
WriterDemo This Visual Basic Script creates an empty text document and inserts text, a table, and a text frame.
Inserting Tables This Delphi program is an application, which creates a connection to the StarOffice server and an empty text document. Further on, you can insert tables specified by the table name and the database pointer prefix, get the database pointer specified by the table name and the cell address, and get the content of a cell specified by the database pointer.

Additional informations:

The OLE Bridge


Author: Bertram Nolte ( 2001-11-28 2:30 PM )
Copyright 2001 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, CA 94303 USA.