Search code examples
javams-worddocx

How to edit docx field contents with Java?


I have a .docx template with fields defined in it. I need to take data inputted by a user in a web-service and insert it into those fields using Java.

My team and I have been researching this for most of the day, and we have been unable to find a straightforward solution to this.

Is there a way to do this relatively easily?

Thanks.

EDIT:

  • After pressing alt+F9, all of the fields display like this: { FORMTEXT }

  • POI doesn't seem to have sufficient support to do this.

  • I was unable to successfully set up the Open Office SDK in Windows XP because I couldn't fulfill all of its dependencies.

  • docx4j may work, but MailMerger in it is currently not filling the fields in with the given data.

  • If I extract the docx and open the word/document.xml file, this is what the XML around one field looks like: http://pastebin.com/uXBtz7X5 (search for FieldName and FieldValue to see where these are defined)


Solution

  • Have a look at Docx4j which you can use to update fields in docx documents there is also and example

    fieldupdater example