Currently i am using content control data binding to generate .docx files. The "repeat" function is so powerful . But i encount a strange issue for TOC.
In the code, i set updateFileds true to update the table of contents for generated document. While the content of TOC keeps the value from original xml.
If you open the out document, you manually update the TOC again, the value will be OK.
The code is as follows:
- Code: Select all
package hana.sdk.test;
import org.docx4j.model.datastorage.CustomXmlDataStorage;
import org.docx4j.model.datastorage.OpenDoPEHandler;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTSettings;
import org.w3c.dom.Element;
import com.sap.hana.sdk.activation.CustomXmlUtils;
public class TestTOC {
public static void main(String[] args) throws Exception {
String inputfilepath = System.getProperty("user.dir") + "/template.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
BooleanDefaultTrue b = new BooleanDefaultTrue();
b.setVal(true);
CTSettings ct = wordMLPackage.getMainDocumentPart().getDocumentSettingsPart().getJaxbElement();
ct.setUpdateFields(b);
wordMLPackage.getMainDocumentPart().getDocumentSettingsPart().setJaxbElement(ct);
String itemId = CustomXmlUtils.getCustomXmlItemId(wordMLPackage).toLowerCase();
CustomXmlDataStoragePart customXmlDataStoragePart = (CustomXmlDataStoragePart) wordMLPackage.getCustomXmlDataStorageParts().get(
itemId);
CustomXmlDataStorage customXmlDataStorage = customXmlDataStoragePart.getData();
org.w3c.dom.Document customXml = customXmlDataStorage.getDocument();
Element nodename = customXml.getDocumentElement();;
for (int i = 0; i < 5; i++) {
Element aa = customXml.createElement("BuildingBlock");
aa.appendChild(customXml.createTextNode("BB header" + i));
nodename.appendChild(aa);
}
OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
odh.preprocess();
String outputfilepath = System.getProperty("user.dir") + "/OUT_TOC.docx";
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}
}
in my expectation, sinece the headers successfully repeat, TOC will update automatically accordling given values.
So would you please see this issue?
Thank you
Mandy