3 things to remember when you play with MS Word docs in Java

I’m playing with my maven plugin (https://github.com/sixro/vodafonecanvassrelease-maven-plugin) and I need to insert texts in a MS Word document. Unfortunately there are some pitfalls you have to know if you use the Apache POI Java library.

Here they are:

  1. if you use object HWPFDocument you’ll lose all images of your document when you save it to a new document.
    Solution: use XWPFDocument instead.
  2. you can change Document Properties (e.g. XWPFDocument.properties.customProperties), then when you open the document you can see that all fields using those properties have not been updated. One solution is to callĀ XWPFDocument.enforceUpdateFields(), but you have to know that with this call, the user will be asked to update all fields at the opening of the doc. This is an issue I’m trying to bypass… probably I have to avoid the use of Document Properties and use something else (any idea?)
  3. when you work with XWPFDocument and document properties, you receive errors like ClassNotFoundException. Solution: if you use Maven you need to import:

    The reasons are explained well on Apache POI FAQ.

One last thing: I have created an abstraction called Word. I think it is useful because you can do anything you want with Apache POI, but this has a cost in clarity of your code.
I hope you’ll find useful!
Bye!

Share Button

Leave a Reply