Page 1 of 1

TOC generation Exception using repeat function

PostPosted: Thu Jun 06, 2013 8:24 pm
by yangmandy
Hi Jason,

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

Re: TOC generation Exception using repeat function

PostPosted: Fri Jun 07, 2013 3:35 pm
by sreejiths_123
Hi ,

Please try using this code

Code: Select all
public class TestDoc {

   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);
         MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
        
       //  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();
         BindingHandler.applyBindings(wordMLPackage);
         RemovalHandler rh = new RemovalHandler();
        rh.removeSDTs(wordMLPackage, Quantifier.ALL);
        
        
         DocumentSettingsPart dsp = documentPart.getDocumentSettingsPart();
         CTSettings objCTSettings = dsp.getJaxbElement();
        
         ObjectFactory factory = Context.getWmlObjectFactory();
         CTView ctView = factory.createCTView();
         ctView.setVal(STView.PRINT);
         
         objCTSettings.setView(ctView);

        
         objCTSettings.setUpdateFields(b);
        dsp.setJaxbElement(objCTSettings);
        documentPart.addTargetPart(dsp);

         String outputfilepath = System.getProperty("user.dir") + "/OUT_TOC.docx";
         SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
         saver.save(outputfilepath);

      }
   
}