Saturday, January 23, 2010

OpenOffice 3.1.1 in a Java 6 Applet

There is a tantalising set of documents on the OpenOffice.org wiki about how you can use the officebean.jar which comes with all OpenOffice installations to display documents in an applet. The problem is, its out of date, and the link to the source is dead. But don't let that stop you...

Note
These are instructions for Windows XP / Java 6 / OpenOffice 3.1.1. I'm sure they can be adapted for other operating systems / platforms.

Pre-Requisites
The Code
Firstly I created a new Java Class Library project in Netbeans called OOoApplet. To this I added a single class which I called "OOoBeanViewer.java". I then (lazily) went looking for the example code. I found it on Koders.com here. I cut and pasted this into my OOoBeanViewer.java stub in Netbeans.

The Dependencies
In order for this to compile and run I had to add the following dependencies:
  • C:\Program Files\OpenOffice.org 3\Basis\program\classes\officebean.jar
  • C:\Program Files\OpenOffice.org 3\Basis\program\classes\unoil.jar
  • C:\Program Files\OpenOffice.org 3\URE\java\ridl.jar
  • C:\Program Files\OpenOffice.org 3\URE\java\jurt.jar
  • C:\Program Files\OpenOffice.org 3\URE\java\juh.jar
The Environment
Because Open Office doesn't really run in the applet (rather we start it, and then redirect its display to the applet) we need so set things up so that the OOoBean knows where to look. This wa the hardest part as the assumption in the docs I could find was that this demo should just work. It didn't for me.

To get it to work I needed to add a Windows environment variable called UNO_PATH and set its value to be the path to the program directory of your OpenOffice install. I set mine to: "C:\Program Files\OpenOffice.org 3\program"

To get this to stick I then had to reboot. A check "echo %UNO_PATH%" at the command line showed that this had worked.

Running It
All that was left was to compile and run. This was done simply using Netbeans. The result is a mostly empty Applet window with some buttons down the side. Fear not! You need to create a new document to see what you desire. This is what you get if you select "New Document ... > Text Document":