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

6 comments:

Robson Peixoto said...

How to make it work on Mac OS X without OOoBean?

Norbert Neubauer said...

Hi,
The links in your post are no longer valid. Could you please post some examples?

I am mostly interested in the code you mentioned here "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.". I can't find working example and would be really grateful if you could share the code, thanks.

Norbert.

Andrew Law said...

Hi Norbert. I think this is what you need: http://hg.services.openoffice.org/cws/adc27/raw-file/44ff200e2e39/odk/examples/DevelopersGuide/OfficeBean/OOoBeanViewer.java

HTH

Cheers, Andrew

Norbert Neubauer said...

Hi,
Thank you for prompt answer :-)

One more question. Since you've already made it running it probably would not be a problem answering.

I am trying to quickly run this code in my Netbeans and then further adjust to my needs, if succeed.
I pasted the code and added officebean.jar to my libraries.

Now the Netbeans complains:

buffer = aBean.storeToByteArray(null, null); => The type of storeToByteArray(byte[],PropertyValue[]) is erroneous
aBean.loadFromByteArray(buffer, null); => The type of loadFromByteArray(byte[],PropertyValue[]) is erroneous
aBean.loadFromURL(url, null); => The type of loadFromURL(String,PropertyValue[]) is erroneous
com.sun.star.frame.XDesktop xDesktop = null; => package com.sun.star.frame does not exist


Could you advice how to fix the errors and which jars should be added to the libraries?

Thanks in advance

Brgs
Norbert

Andrew Law said...

Hi Norbert. I think the list or jars in the post should be the ones you need. Unfortunately it's a long time since I had this running and I've not got the code on my current laptop.

Andrew

Norbert Neubauer said...

Thank you for your help,
you are absolutely right, the libraries are given in the beginning of your post, I read, read, but missed them ;-)



I successfully run your code from Netbeans (the previous errors are warnings only). The crucial is to set the UNO_PATH, but also PATH, in my case I had to setup path to both:

C:\Program Files (x86)\OpenOffice.org 3\program;

and
C:\Program Files (x86)\OpenOffice.org 3\Basis\program\classes;

Thank you for your help and sharing this !!!