Search code examples
javadocxdocx4j

Variables did not replaced


I wrote some code to replace variables in docx tamplate file header.

List<SectionWrapper> sectionWrappers = this.wordMLPackage.getDocumentModel().getSections();
for (SectionWrapper sw : sectionWrappers) {
    HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
    HeaderPart defaultHP = hfp.getDefaultHeader();
        if (defaultHP != null) {
            defaultHP.variableReplace(getVariablesForChange());
            if (hfp.getFirstHeader() != null) {
              hfp.getFirstHeader().variableReplace(getVariablesForChange());
            }
        }
}

getVariablesForChange() is a Map has contains the variables and values. When I running the unit test the replace is corectly fine but I use this in my web application on Tomee Plume the variables does not replaced. For example the variable is: ${TOCHANGE} it looks like this after change TOCHANGE.

Docx4j version is: 3.3.6

Please help me to resolve this issue.


Solution

  • It won't work if your KEY is split across separate runs in your docx.

    See https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/datastorage/migration/VariablePrepare.java