[ACCEPTED]-Is there a Java API that can create rich Word documents?-doc

Accepted answer
Score: 56

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

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

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

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

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

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

The 2 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 1 are some useful web sites:

Score: 30

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

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

Score: 9

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

I implemented this API 9 called Java2Word. with a few lines of code, you 8 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 7 is some examples how to use. Basically you 6 will need one jar file. Let me know if you 5 need any further information how to set 4 it up.

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

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

cheers Leonardo

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

Score: 5

Try Aspose.Words for Java, it runs on any 22 OS where Java is installed.

It will output 21 the document to DOC, DOCX or RTF if you 20 need an MS Word output format. All are supported 19 equally well.

Using this API you can create 18 a document from scratch, literally from 17 nodes and set their formatting properties. You 16 can also use a DocumentBuilder which provides 15 higher level methods such as create a table 14 row, insert a field etc. Or you can copy/join/move 13 portions between existing pre created document, say 12 you want to assemble a contract, just grab 11 and copy pieces from several documents and 10 Aspose.Words will merge styles, list formatting 9 etc properly in the resulting document.

You 8 will be able to insert a TOC field using 7 Aspose.Words, but as of today, the TOC field 6 will require a field update when the document 5 is opened in Microsoft Word. However, we 4 are going to release full support for TOC 3 fields early in 2010. E.g. it will build 2 complete TOC as MS Word does it.

I'm on the 1 Aspose.Words team.

Score: 4

Try Aspose.Words for java.

Aspose.Words for Java is an 8 advanced (commercial) class library for 7 Java that enables you to perform a great 6 range of document processing tasks directly 5 within your Java applications.

Aspose.Words 4 for Java supports DOC, OOXML, RTF, HTML 3 and OpenDocument formats. With Aspose.Words 2 you can generate, modify, and convert documents 1 without using Microsoft Word.

Score: 4

It was mentioned only briefly once, so I'd 11 like to call out the docx4j library, as 10 I've had more success with docx4j than anything 9 else. Apache POI's support for Word documents 8 isn't very good. Also, unlike Aspose.Words, docx4j 7 is an open source library.

The only drawback 6 is with docx4j you have to create Office 5 Open XML (docx) format documents rather 4 than OLE2-based (doc) format documents. This 3 is the default format for Word 2007, but 2 Word 2003 and earlier users will need to 1 install a compatibility pack.

Score: 3

I've used Aspose.Words to do mail merge in .NET. I 1 believe that they also have a Java version.

Score: 2

You can use a Java COM bridge like JACOB. If 2 it is from client side, another option would 1 be to use Javascript.

Score: 2

There's a tool called JODConverter which 8 hooks into open office to expose it's file 7 format converters, there's versions available 6 as a webapp (sits in tomcat) which you post 5 to and a command line tool. I've been firing 4 html at it and converting to .doc and pdf 3 succesfully it's in a fairly big project, haven't 2 gone live yet but I think I'm going to be 1 using it. http://sourceforge.net/projects/jodconverter/

Score: 1

I have developed pure XML based word files 10 in the past. I used .NET, but the language 9 should not matter since it's truely XML. It 8 was not the easiest thing to do (had a project 7 that required it a couple years ago.) These 6 do only work in Word 2007 or above - but 5 all you need is Microsoft's white paper 4 that describe what each tag does. You can 3 accomplish all you want with the tags the 2 same way as if you were using Word (of course 1 a little more painful initially.)

Score: 1

docx4j or poi, both of which are ASL v2

@wondersofcomputing: iText 1 is actually free and open source

Score: 1

iText is really easy to use.

If you requiere doc 3 files you can call abiword (free lightweigh multi-os 2 text procesor) from the command line, it 1 has several conversion format convert options.

Score: 1

After a little more research, I came across 23 iText, a PDF and RTF-file creation API. I 22 think I can use the RTF generation to create 21 a Doc-readable file that can then be edited 20 using Doc and re-saved.

Anyone have any experience 19 with iText, used in this fashion?

Bill, the 18 POI and iText API are very similar from 17 a programming perspective. I've worked with 16 both in the past and found them both easy 15 to use and well documented.

With iText you 14 gain the advantage of being able to switch 13 between formats (RTF and PDF) with minor 12 change to the code. If I remember correctly 11 the content is laid out using the same calls 10 and then set as PDF or RTF using a few lines 9 of code.

However I believe the formatting 8 in RTF is limited compared to DOC. I don't 7 know if you'll be able to implement the 6 advanced features you are looking for (tables, inline 5 images) without a decent amount of hassle, if 4 at all.

Given what you said that about HWPF 3 not having enough functionality for your 2 needs (I've only dealt with the Excel side 1 of POI) your best bet may be to convince the powers that be that PDF is the best technology for the job.

Score: 0

Yet another possibility, since this is a 11 web app.

I was able to render an HTML page 10 with the MIME type set to "application/msword", which 9 caused the browser to spawn Word which imported 8 the html just fine, allowing edits and saving 7 just as if I'd output a real Word doc.

Tables 6 work fine, but images I hadn't gotten working 5 yet. It may be as easy as just an tag 4 in the HTML, or I may have to stream a separate 3 part of the response containing the image 2 data in binary, or some other method I haven't 1 come up with yet. :)

Score: 0

Even though this is much later than the 7 request, it might help others. Docmosis provides 6 a Java API for creating documents in doc,pdf,odt 5 format using documents as templates. It 4 uses OpenOffice as the engine to perform 3 the format conversions. Document manipulation 2 and population is performed by Docmosis 1 itself.

More Related questions