Integrate OpenOffice with Java without Installing OpenOffice

Until a few days ago, I've always needed to work with the rather cumbersome Office Bean and UNO Runtime when integrating OpenOffice into a Java application. I also had to configure a whole bunch of things to force OpenOffice to play nicely with the Java integration. Two days ago, however, I found out about ODF Toolkit. It seems to be a relatively new project, independent since last year some time, though I could be wrong.

What's especially interesting is the ODFDOM: ''ODFDOM is an OpenDocument (ODF) framework. It's purpose is to provide an easy common way to create, access and manipulate ODF files, without requiring detailed knowledge of the ODF specification. It is designed to provide the ODF developer community an easy lightwork programming API, portable to any object-oriented language.''

Here's a snippet of it in action:

public static void main(String[] args) {
try {
OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/test.ods"));
OdfFileDom odfContent = odfDoc.getContentDom();
XPath xpath = odfDoc.getXPath();
DTMNodeList nodeList = (DTMNodeList) xpath.evaluate("//table:table-row/table:table-cell[1]", odfContent, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node cell = nodeList.item(i);
if (!cell.getTextContent().isEmpty()) {
System.out.println(cell.getTextContent());
}
}
} catch (Exception ex) {
//Handle...
}
}

Let's assume that the 'test.ods' file above has this content:

From the above, the code listing would print the following:

Cuthbert
Algernon
Wilbert

And, as a second example, here's me reading the first paragraph of an OpenOffice Text document:

public static void main(String[] args) {
try {
OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/chapter2.odt"));
OdfFileDom odfContent = odfDoc.getContentDom();
XPath xpath = odfDoc.getXPath();
OdfParagraphElement para = (OdfParagraphElement) xpath.evaluate("//text:p[1]", odfContent, XPathConstants.NODE);
System.out.println(para.getFirstChild().getNodeValue());
} catch (Exception ex) {
//Handle...
}
}

On my classpath I have "odfdom.jar" and "xerces-2.8.0.jar". I don't necessarily have OpenOffice installed, which means I can very easily process a whole bunch of spreadsheets (or other OpenOffice output) without (a) installing OpenOffice and (b) faster than I would otherwise do, since OpenOffice doesn't need to be started up, via the Office Bean or otherwise. In fact, Aljoscha Rittner from Sepix, who told me about this project and who is using it in his commercial applications, reports that his processing has sped up to a fraction of the original, also because he doesn't need to handle the situation where OpenOffice would crash randomly in the middle of long running processes, such as during the night when there's no human interaction for restarting it.

 

AttachmentSize
fig-1.png9.53 KB
0
Average: 4.3 (3 votes)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

arittner replied on Sun, 2009/02/08 - 11:33am

Hi Geertjan!

I am very pleased that you like the odf-like project.

best regards,
  josh.

 

othman El moulat replied on Sun, 2009/02/08 - 7:28pm

i remember few months back i was working on a Openoffice.org combined chart project.
I thought i could use jfeechart as the chart rendering engine inside OO.o. for this i knew i will need a open source library that reads/writes Open document format and marry jfreechart with this ODF API. After some research, I found an open source project odfchartbuilder
this project is still in early phases doing this same task of drawing OO.o charts using jfreechart as the rendering engine and ODF interface to talk to OO.o spreadsheet documents.
Anyway Thanks for sharing this Interesting article.

othman.

arittner replied on Mon, 2009/02/09 - 5:02am in response to: othmanelmoulat

Hi Othman!

I work with the ODFDOM since 2 month and I'm very happy with this library. It is very easy to paste images in odf documents. I use the cobra renderer to make "pictures" from HTML-Mails and paste this rendered images into ODF-Writer documents. This works very well. 

Here is an sample about pasting an image:

// 1st step: Create a Frame-Style for Images:

OdfAutomaticStyles contentAutoStyles = odfDom.getOrCreateAutomaticStyles();
OdfStyle style = (OdfStyle) OdfElementFactory.createOdfElement(odfDom, OdfStyle.ELEMENT_NAME);
style.setName("fr1"); // should be an unique identifier
style.setFamily(OdfStyleFamily.Graphic);
style.setParentStyleName("Graphics"); // my frame contains iamges

OdfGraphicProperties property = (OdfGraphicProperties) OdfElementFactory.createOdfElement(odfDom, OdfGraphicProperties.ELEMENT_NAME);
property.setAttribute("style:protect", "size position"); // The user can't change size or position
style.appendChild(property);

contentAutoStyles.appendChild(style); // append my new style

// 2nd step: Paste a Image

OdfFrame odfFrame = (OdfFrame) OdfElementFactory.createOdfElement(odfDom, OdfFrame.ELEMENT_NAME);
odfFrame.setDrawStyleName("fr1"); // referer to the declared style
odfFrame.setAttribute("text:anchor-type", "as-char"); // for an easier positioning
odfFrame.setAttribute("svg:width", "16cm"); // currently needed (should be calculated)
odfFrame.setAttribute("svg:height", "1.6cm"); // currently needed (should be calculated)
textBit.getParentNode().appendChild(odfFrame);
OdfImage odfImage = (OdfImage) OdfElementFactory.createOdfElement(odfDom, OdfImage.ELEMENT_NAME);
odfImage.insertImage(file.toURI());
odfFrame.appendChild(odfImage);

best regards,
  josh.

Fabrizio Giudici replied on Mon, 2009/02/09 - 5:05am

Great. I'm just going to write some code to import data from some spreadsheet. Usually I hate to install the whole OpenOffice and I had to manually open the .zip with Java I/O, and get the .xml etc... Cumbersome. I wasn't aware of this project and sounds very useful.

othman El moulat replied on Mon, 2009/02/09 - 1:28pm in response to: arittner

Hi Josh,
Excellent source code sample!.thanks for sharing.
a couple of years back, when i first started experimenting with OO UNO API, I was put-off b/c there was No way -at that time- to use UNO OO API without going through the cumbersome installation of OO and OO SDK. The ODF project seems to be the answer for this limitation with OO Java development. I'm really excited and will definitely investigate in more depth the capabilities & features of ODF library.

Regards
othman.

Andy Jefferson replied on Wed, 2009/02/11 - 5:04am

Thanks Geertjan. I just wrote a plugin for DataNucleus allowing persistence of java objects using JDO/JPA APIs to ODF documents (to complement what we already supported - RDBMS, ODBMS, XML, LDAP, Excel, JSON). Handles all simple field types, as well as allowing JDOQL/JPQL querying. Will be improved in future releases to handle relations

-- Andy (DataNucleus)

mwildam replied on Tue, 2009/04/21 - 8:17am

Thanks Geertjan, you saved my day! (And Google of course by showing up this blog post in the search results).

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.