In the current economic climate every business is looking at ways in which it can become as cost effective as possible. One ways is to use open source applications such as OpenOffice.org.
OpenOffice.org is an excellent substitute for Microsoft Office for most users and, of course, this provides a challenge for the C# programmer. It gives them yet another application to automate. Fortunately the challenge is not beyond their abilities and after just a few minutes programming they will have applications that can create and save OpenOffice.org Writer documents.
Adding OpenOffice.org References to a C# Project
The OpenOffice.org namespaces required in any C# application start with the letters “cli” (CLI stands for Common Language Interface). The developer will need to select them all and add them as references (as shown in figure 1 at the bottom of this article).
Using the OpenOffice.org Namespaces
Once the programmer has added the OpenOffice.org CLI namespaces to their project then they can use the necessary namespaces in their application:
And then any required methods and properties can be accessed easily.
Starting OpenOffice.org with C#
The C# application starts OpenOffice.org by using it's bootstrap method:
This will start a new instance of OpenOffice.org if it is not running, or it will obtain an existing instance if it is already open. The next step is to create a new OpenOffice.org service manager:
It's worth noting that the OpenOffice.org CLI uses a lot of casting (the setting and changing of specific types of objects) in order to achieve all of the required functionality.
This sometimes make the code look more complicated than other languages but simply allows the programmer to use the same objects in different ways. Once the programmer has experienced casting then it is actually not very complex, as shown in the the next step where the programmer uses the XmultiServiceFactory to create a desktop :
Any required documents are added to this desktop.
Creating an OpenOffice.org Writer Document with C#
Like many of the CLI operations, the creating of a new Writer looks complicated, but is actually very simple:
The end result of the user running this code is that a blank Writer document is displayed on their screen.
Writing to an OpenOffice.org Writer Document
At it's simplest level writing to an OpenOffice.org Writer document is just a matter of creating a string (which, in this case, will contain two paragraphs):
And then this is written to the body of the document:
Again the casting can be seen in operation – this time setting the document up as a text document.
Saving an OpenOffice.org Writer Document
Saving a file is made complicated not because of the methods used but because the format of the file name . For example, instead of a file being saved as:
it must be saved as:
Therefore, a little text conversion is required:
And then the file is saved to disk:
Here the casting is used to convert the document into an object that can be stored on the computer.
Closing OpenOffice.org
At the moment the user will be able to see the saved document on the screen. However, the developer may wish for the document to be closed once it has been saved. If that's the case then casting is used once more to close the OpenOffice.org application:
And then any memory that's been used can be freed up:
At the end of the process a brand new OpenOffice.org Writer document will have been created on the application user's computer.