1
votes

First i have created a document template file and added a xml to MS Document from XML Mapping Pane of Developer tab.

Xml used for content binding.

<employee>
<name></name>
</employee>

Below mentioned method is used to write xml into doc template. Where this method takes documentForm(result doc file), xmlSchemaFilePath(created xml path), templateForm(templete file) as arguments.

 public String fillXMLBasedForm(String documentForm, String xmlSchemaFilePath, String templateForm)
        throws Exception {

    try {
        WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(templateForm));
        FileInputStream xmlStream = new FileInputStream(xmlSchemaFilePath);
        Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML);
        Docx4J.save(wordMLPackage, new FileOutputStream(new File(documentForm)), Docx4J.FLAG_NONE);
    }
    catch(Exception e) {
        e.printStackTrace();
    }

    return documentForm;
}

Once i open the generated document file then got "Word found unreadable content.." popup. once i proceed as trusted document then found no issue in content because for testing purpose written only single string to it.

XML added via code. 

<employee>
<name>Vivek Singh</name>
</employee>
1
Please try again, using an up to date docx4j (8.1.5 or 8.1.6 at the time of writing)JasonPlutext
@JasonPlutext what are the minimum jars to add because i can see number of Docx4j. search.maven.org/search?q=g:org.docx4jVivek Singh
Thanks @JasonPlutext, issued is resolved by using docx4j(v8.1.5).Vivek Singh

1 Answers

1
votes

Here is the minimum required jars that I have used to solve this problem.

commons-compress-1.18
commons-io-2.6
docx4j-core-8.1.5
docx4j-JAXB-Internal-8.1.5
docx4j-openxml-objects-8.1.5
slf4j-api-1.7.26
xalan-interpretive-8.0.0
xalan-serializer-8.0.0