We are going to have some template docx file, where will be some tags like ${content}. I need to replace this tags with HTML.
For this purpose I want to use altChunk element in XWPFDocument. Following answer in How to add an altChunk element to a XWPFDocument using Apache POI, I could place altChunk in the end of docx.
How can I replace my tag with it? Or could I use any other libraries, may be docx4j?
UPD: Template docx files with tags are created by end users with MS Word and looks like:
If "${content}" is in a IBodyElement of it's own, then solving that requirement by finding that
IBodyElement
, creating aXmlCursor
, inserting thealtChunk
, then removing theIBodyElement
would be possible.The following code demonstrates this by extending the example in How to add an altChunk element to a XWPFDocument using Apache POI. It provides a method for replacing a found
IBodyElement
, which contains a special text, with aaltChunk
which references aMyXWPFHtmlDocument
. It usesXmlCursor
to get the needed position in the text body. The usage ofXmlCursor
is commented in the code.template.docx:
Code:
result.docx: