WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
Body body = wmlDocumentEl.getBody();
ObjectFactory factory = Context.getWmlObjectFactory();
/* Create the following:
*
<w:p>
<w:r>
<w:fldChar w:dirty="true" w:fldCharType="begin"/>
<w:instrText xml:space="preserve">TOC \o "1-3" \h \z \ u \h</w:instrText>
</w:r>
<w:r/>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p> */
P paragraphForTOC = factory.createP();
R r = factory.createR();
FldChar fldchar = factory.createFldChar();
fldchar.setFldCharType(STFldCharType.BEGIN);
fldchar.setDirty(true);
r.getContent().add(getWrappedFldChar(fldchar));
paragraphForTOC.getContent().add(r);
R r1 = factory.createR();
Text txt = new Text();
txt.setSpace("preserve");
txt.setValue("TOC \\o \"1-3\" \\h \\z \\u \\h");
r.getContent().add(factory.createRInstrText(txt) );
paragraphForTOC.getContent().add(r1);
FldChar fldcharend = factory.createFldChar();
fldcharend.setFldCharType(STFldCharType.END);
R r2 = factory.createR();
r2.getContent().add(getWrappedFldChar(fldcharend));
paragraphForTOC.getContent().add(r2);
body.getContent().add(paragraphForTOC);
documentPart.addStyledParagraphOfText("Heading1", "Hello 1");
documentPart.addStyledParagraphOfText("Heading2", "Hello 2");
documentPart.addStyledParagraphOfText("Heading3", "Hello 3");
documentPart.addStyledParagraphOfText("Heading1", "Hello 1");