A while back, the OpenXmlDeveloper.org website offered an example of how to create a WordProcessingML document from within Java code. I even referenced it briefly here. Remember, WordProcessingML is the document format produced and consumed by Microsoft Word 2007, and the format will also be supported by back versions of Word via the freely downloadable compatibility pack.
I looked into that post, and found it to be pretty simplistic. Essentially, it is a guide for using the java.util.zip.* packages that are part of Java, to create a valid .docx file. The content of the document itself – the formatted text, the embedded image – in the post I referenced, all that content is manually constructed via text editors. So the post is not so much about “creating” a document, as it is about zipping up the existing parts of a WordProcessingML document.
I think a better, more thorough approach for Java is required. What is needed is a class library for Java that parallels the System.IO.Packaging namespace in .NET 3.0. Something that allows a Java application to define the parts of a document, the relationships, and so on… programmatically, much as this article describes for a .NET application.
Given the support for zip compression already in Java, and the fairly clear packaging rules for OpenXML, this extra step shouldn’t be too hard to do. Once you have the packaging utility classes, then you can use the template approach I described a while ago, for WordML (the XML format from Word 2003), to create the content that gets zipped up.
What does this get you? the ability to quickly generate OpenXML docs from Java, using a higher-level API that correctly models the packaging metaphor in the OpenXML spec. In theory you would be able to do this from any Java application, whether a thick client, a server-side app, or an agent or batch job that runs with no UI at all.
Sorry, no example code, yet. I’ll look into either creating a sample or finding someone else to do it. Any volunteers?