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:
- 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.
- 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?)
- when you work with XWPFDocument and document properties, you receive errors like ClassNotFoundException. Solution: if you use Maven you need to import:
Maven dependencyXHTML12345<dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.1</version></dependency>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!