Creating an OpenOffice Writer Document with C#

How to Use C# Code to Create and Save OpenOffice.org Documents

© Mark Alexander Bain

Jun 8, 2009
Creating an OpenOffice Writer Document with C#, Mark Alexander Bain
C# programmer can now delve into the world of open source applications such as OpenOffice.org Writer by making use of the CLI - the Common Language Interface.

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:

using System;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.beans;

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:

XComponentContext oStrap = uno.util.Bootstrap.bootstrap();

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:

XMultiServiceFactory oServMan = (XmultiServiceFactory) oStrap.getServiceManager();

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 :

XComponentLoader oDesk = (XComponentLoader) oServMan.createInstance("com.sun.star.frame.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:

string url = @"private:factory/swriter";
PropertyValue[] propVals = new PropertyValue[0];
XComponent oDoc = oDesk.loadComponentFromURL(url, "_blank", 0, propVals);

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):

string docText = "This will be my first paragraph.\n\r";
docText += "This will be my second paragraph.\n\r";

And then this is written to the body of the document:

((XTextDocument)oDoc).getText().setString(docText);

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:

C:\Reports\test.odt

it must be saved as:

file:///C:/Reports/test.odt

Therefore, a little text conversion is required:

string fileName = @"C:\Reports\test.odt";
fileName = "file:///" + fileName.Replace(@"\", "/");

And then the file is saved to disk:

((XStorable)oDoc).storeAsURL(fileName, propVals);

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:

((Xcomponent)oDoc).dispose();

And then any memory that's been used can be freed up:

oDoc = null;

At the end of the process a brand new OpenOffice.org Writer document will have been created on the application user's computer.


The copyright of the article Creating an OpenOffice Writer Document with C# in C Programming is owned by Mark Alexander Bain. Permission to republish Creating an OpenOffice Writer Document with C# in print or online must be granted by the author in writing.


Creating an OpenOffice Writer Document with C#, Mark Alexander Bain
Figure 1 Adding OpenOffice References to C#, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo