Is there a Java API that can create rich Word documents?

In 2007 my project successfully used OpenOffice.org's Universal Network Objects (UNO) interface to programmatically generate MS-Word compatible documents (*.doc), as well as corresponding PDF documents, from a Java Web application (a Struts/JSP framework).

OpenOffice UNO also lets you build MS-Office-compatible charts, spreadsheets, presentations, etc. We were able to dynamically build sophisticated Word documents, including charts and tables.

We simplified the process by using template MS-Word documents with bookmark inserts into which the software inserted content, however, you can build documents completely from scratch. The goal was to have the software generate report documents that could be shared and further tweaked by end-users before converting them to PDF for final delivery and archival.

You can optionally produce documents in OpenOffice formats if you want users to use OpenOffice instead of MS-Office. In our case the users want to use MS-Office tools.

UNO is included within the OpenOffice suite. We simply linked our Java app to UNO-related libraries within the suite. An OpenOffice Software Development Kit (SDK) is available containing example applications and the UNO Developer's Guide.

I have not investigated whether the latest OpenOffice UNO can generate MS-Office 2007 Open XML document formats.

The important things about OpenOffice UNO are:

  1. It is freeware
  2. It supports multiple languages (e.g. Visual Basic, Java, C++, and others).
  3. It is platform-independent (Windows, Linux, Unix, etc.).

Here are some useful web sites:

  • Open Office home
  • Open Office UNO Developer's Guide
  • OpenOffice Developer's Forum (especially the "Macros and API" and "Code Snippets" forums).

I think Apache POI can do the job. A possible problem depending on the usage your aiming to may be caused by the fact that HWPF is still in early development.

HWPF is the set of APIs for reading and writing Microsoft Word 97(-XP) documents using (only) Java.


You could use this: http://code.google.com/p/java2word

I implemented this API called Java2Word. with a few lines of code, you can generate one Microsoft Word Document.

Eg.:

IDocument myDoc = new Document2004();
myDoc.getBody().addEle(new Heading1("Heading01"));
myDoc.getBody().addEle(new Paragraph("This is a paragraph...")

There is some examples how to use. Basically you will need one jar file. Let me know if you need any further information how to set it up.

*I wrote this because we had one real necessity in a project. More in my blog:

http ://leonardo-pinho.blogspot.com/2010/07/java2word-word-document-generator-from.html *

cheers Leonardo

Edit : Project in link moved to https://github.com/leonardoanalista/java2word