Is there a Java API that can create rich Word documents?
Solution 1
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:
- It is freeware
- It supports multiple languages (e.g. Visual Basic, Java, C++, and others).
- 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).
Solution 2
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.
Solution 3
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
Solution 4
Try Aspose.Words for Java, it runs on any OS where Java is installed.
It will output the document to DOC, DOCX or RTF if you need an MS Word output format. All are supported equally well.
Using this API you can create a document from scratch, literally from nodes and set their formatting properties. You can also use a DocumentBuilder which provides higher level methods such as create a table row, insert a field etc. Or you can copy/join/move portions between existing pre created document, say you want to assemble a contract, just grab and copy pieces from several documents and Aspose.Words will merge styles, list formatting etc properly in the resulting document.
You will be able to insert a TOC field using Aspose.Words, but as of today, the TOC field will require a field update when the document is opened in Microsoft Word. However, we are going to release full support for TOC fields early in 2010. E.g. it will build complete TOC as MS Word does it.
I'm on the Aspose.Words team.
Solution 5
It was mentioned only briefly once, so I'd like to call out the docx4j library, as I've had more success with docx4j than anything else. Apache POI's support for Word documents isn't very good. Also, unlike Aspose.Words, docx4j is an open source library.
The only drawback is with docx4j you have to create Office Open XML (docx) format documents rather than OLE2-based (doc) format documents. This is the default format for Word 2007, but Word 2003 and earlier users will need to install a compatibility pack.
Related videos on Youtube
jciconsult
I'm a 30-year programming veteran: started with C/C++ on Star Wars SDI sim Video game programming on Genesis, PS1, and PC Now I'm a web-app developer in Java/Groovy and AWS Currently spinning up on custom AEM dev I enjoy math puzzles, programming, and the like. http://discreteideas.tankerbay.com - my old math blog http://www.linkedin.com/in/billjames http://www.twitter.com/billjamesdev
Updated on July 08, 2022Comments
-
jciconsult almost 2 years
I have a new app I'll be working on where I have to generate a Word document that contains tables, graphs, a table of contents and text. What's a good API to use for this? How sure are you that it supports graphs, ToCs, and tables? What are some hidden gotcha's in using them?
Some clarifications:
- I can't output a PDF, they want a Word doc.
- They're using MS Word 2003 (or 2007), not OpenOffice
- Application is running on *nix app-server
It'd be nice if I could start with a template doc and just fill in some spaces with tables, graphs, etc.
Edit: Several good answers below, each with their own faults as far as my current situation. Hard to pick a "final answer" from them. Think I'll leave it open, and hope for better solutions to be created.
Edit: The OpenOffice UNO project does seem to be closest to what I asked for. While POI is certainly more mainstream, it's too immature for what I want.
-
jciconsult about 12 yearsNot sure how closing this question 30 months after it was last edited, and over 3 years after it was originally asked is going to be very productive. If I changed the title to "How do I create rich Word documents with a Java API?" would that fix this?
-
Supun Sameera about 10 yearsin case some one need a full review of the available java api esupu.com/open-source-office-document-java-api-review
-
edi9999 over 9 yearsYou might have a look at docxtemplater, https://github.com/edi9999/docxtemplater/ which is a library I created to generate docx from docx templates
-
Sjoerd Pottuit over 5 yearsThe question should be reopened. The question has been reworded to fit the rules, therefore, I would like to call whoever has the power to reopen this question. Before the question was: What's a good Java API for creating Word documents? Now the question is: Is there a Java API that can create rich Word documents? The question is now clearly pointing to what the asking person wants: creating Word documents with graphs, ToCs, and tables. The question also no longer asks for a "good" Java API. Good is different for everyone.
-
jciconsult over 15 yearsAny knowledge of graph and table possibilities? How about tables of contents? Anyone have real experience doing those things in POI?
-
jciconsult over 15 yearsLooking at the documentation for POI, it seems this HWPF is very early in development, mainly allowing for reading text out of a .doc, not really for dynamic creation of "complex" documents.
-
jciconsult over 15 yearsThanks, but it looks like this would require running on a windows machine, no? I clarified the host machine OS after reading this, but thanks for the info.
-
Brian Agnew almost 15 yearsI don't believe it handles the more complex graphs/tables etc.
-
jciconsult over 14 yearsSo far, this is the most compatible with the toolset I asked for. I'm going to mark it "accepted". Though I fully recognize that POI is more mainstream, it just doesn't have the functionality I want yet.
-
AlfaTeK about 14 yearsI have used iText to export to RTF and it's a bit flaky: TOC for example don't work that well and it's really not very easy to use (docs lacking)
-
hello_earth over 13 yearsUsing JACOB on the web-server machine would require Microsoft Word itself to be installed on it, because creating and manipulating Word documents through COM interface requires bringing up actual instances of Word application. In general, such use of Word+COM on a multi-user server is quite problematic because Word is not designed for such use - for instance duplicating parts of the document is traditionally done using Selection object and Windows clipboard, which is unthinkable in web-server machine setting. i have been quite burned by this (although having found some tweaks)
-
Stein G. Strindhaug over 13 yearsHave you tested it using actual MS Word? I've managed to create files that OpenOffice and LibreOffice can read but not MSWord on windows. (I've reported this issue at code.google.com/p/java2word/issues/detail?id=16 )
-
Ashika Umanga Umagiliya about 13 yearsI cannot open java2word generated files using OpenOffice ? Works fine with Office 2010
-
MaheshVarma almost 11 yearsDoes it support for creation of .docx files ? @Leonardo
-
vhunsicker over 8 yearsNe wproject home: github.com/leonardoanalista/java2word
-
JasonPlutext over 5 yearsPOI or docx4j? stackoverflow.com/questions/15013837/…