Sunday, January 01, 2012

Merging Word documents with docx4j

Recently I needed to merge some Word .docx  documents and the tools that we chose for this was docx4j (www.docx4java.org). This is library for Java for working with Microsoft Open XML. There we two things that we needed to accomplish:

  1. Binding XML to various templates
  2. Merging documents as a result of the binding

I will write about merging documents as binding them is already explained well in docx4j site. Author of docx4j also offers commercial package for merging documents but if you want to try it for yourself, here are couple of things that I managed to do and got pretty decent results.

In version 2.7.1 docx4j you can work with java.util.File or java.io.InputStream. First one will do a god job if you have file present in your drive and second one if you keep content in the database (for example).  When merging Word documents you have to take care of relationships in the document itself. There are several elements that have relationships that can span through the document, but we were interested in just a few of them (images, footers and headers). It is worth to mention that if you miss one relationship, your document will be unreadable (in most cases). These are listed as resources and have references that you can use in your paragraphs.

So, to start we would:

  1. Load our initial file in WordprocessingMLPackage (this is the file where we want to attach the rest of the files, so in the end they look as one)
  2. Create unique section template
  3. Reset sections (this will serve the purpose of removing all references from the existing template, remember that section defines page layout)
  4. Remove body section (we can add this in the end)
  5. Loop through the attachment files (if you do not have sections separating pages, you might add page breaks)
  6. Copy relationships that you are interested in
  7. Copy elements
  8. If you do not want page breaks, then you can add empty section
  9. Add body section
  10. Reapply all headers and footers to empty sections

This all might sound complicated, but in the end, once you get to know the structure of the WordprocessingMLPackage, it becomes easier.

These are the code snippets that might be useful:

Note: All code displayed in upper window is property of Sapiens North America

3 comments:

  1. Hi Cavlin, I'm new with docx4j and I have problem with merging .docx files using docx4j. I read your code snippets but I don't know how to make it works. Can you please send the MergeUtil class and the interface ImergeUtil to mrcancer91@gmail.com? Or give some more code in snippest?
    I'm sorry if my English is bad.
    Thank you very much!

    ReplyDelete
  2. Hi,

    Thanks for leaving the message. Unfortunately I do not have the code anymore as I no longer work for the company where I developed this code. The purpose of the code in this text was to show in general what approach you needed to merge two Word documents. End code is much more complex. Knowledge of the Word document structure is very important to accomplish this. That is why Plutext is offering merge utility commercially as an extension to docx4j. You really need to gather resources from both documents, make sure sections are properly placed and structured, replace or add resources in the document that you want to use as a finished template.

    Regards

    ReplyDelete
  3. Hey, your code really helped me to alter my document. I got a problem, how to insert TOC in second page?

    ReplyDelete