| 1 | package org.docx4j.samples; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | |
|---|
| 5 | import javax.xml.bind.JAXBElement; |
|---|
| 6 | import javax.xml.namespace.QName; |
|---|
| 7 | |
|---|
| 8 | import org.docx4j.jaxb.Context; |
|---|
| 9 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 10 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 11 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 12 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 13 | import org.docx4j.wml.Body; |
|---|
| 14 | import org.docx4j.wml.FldChar; |
|---|
| 15 | import org.docx4j.wml.ObjectFactory; |
|---|
| 16 | import org.docx4j.wml.P; |
|---|
| 17 | import org.docx4j.wml.R; |
|---|
| 18 | import org.docx4j.wml.STFldCharType; |
|---|
| 19 | import org.docx4j.wml.Text; |
|---|
| 20 | |
|---|
| 21 | public class AddTOC { |
|---|
| 22 | |
|---|
| 23 | public static void main(String[] args) { |
|---|
| 24 | // TODO Auto-generated method stub |
|---|
| 25 | try { |
|---|
| 26 | |
|---|
| 27 | ObjectFactory factory = Context.getWmlObjectFactory(); |
|---|
| 28 | |
|---|
| 29 | P p = factory.createP(); |
|---|
| 30 | R r = factory.createR(); |
|---|
| 31 | |
|---|
| 32 | FldChar fldchar = factory.createFldChar(); |
|---|
| 33 | fldchar.setFldCharType(STFldCharType.BEGIN); |
|---|
| 34 | fldchar.setDirty(true); |
|---|
| 35 | r.getRunContent().add(getWrappedFldChar(fldchar)); |
|---|
| 36 | p.getParagraphContent().add(r); |
|---|
| 37 | |
|---|
| 38 | R r1 = factory.createR(); |
|---|
| 39 | Text txt = new Text(); |
|---|
| 40 | txt.setSpace("preserve"); |
|---|
| 41 | txt.setValue("TOC \\o \"1-3\" \\h \\z \\u \\h"); |
|---|
| 42 | r.getRunContent().add(factory.createRInstrText(txt) ); |
|---|
| 43 | p.getParagraphContent().add(r1); |
|---|
| 44 | |
|---|
| 45 | FldChar fldcharend = factory.createFldChar(); |
|---|
| 46 | fldcharend.setFldCharType(STFldCharType.END); |
|---|
| 47 | R r2 = factory.createR(); |
|---|
| 48 | r2.getRunContent().add(getWrappedFldChar(fldcharend)); |
|---|
| 49 | p.getParagraphContent().add(r2); |
|---|
| 50 | |
|---|
| 51 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 52 | MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 53 | |
|---|
| 54 | org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement(); |
|---|
| 55 | Body body = wmlDocumentEl.getBody(); |
|---|
| 56 | body.getEGBlockLevelElts().add(p); |
|---|
| 57 | |
|---|
| 58 | documentPart.addStyledParagraphOfText("Heading1", "Hello 1"); |
|---|
| 59 | documentPart.addStyledParagraphOfText("Heading2", "Hello 2"); |
|---|
| 60 | documentPart.addStyledParagraphOfText("Heading3", "Hello 3"); |
|---|
| 61 | documentPart.addStyledParagraphOfText("Heading1", "Hello 1"); |
|---|
| 62 | |
|---|
| 63 | wordMLPackage.save(new File("c:\\tmpxml\\AddTOC.docx")); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | } catch (InvalidFormatException e) { |
|---|
| 67 | e.printStackTrace(); |
|---|
| 68 | }catch (Exception e) { |
|---|
| 69 | e.printStackTrace(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | public static JAXBElement getWrappedFldChar(FldChar fldchar) { |
|---|
| 76 | |
|---|
| 77 | return new JAXBElement( new QName(Namespaces.NS_WORD12, "fldChar"), |
|---|
| 78 | FldChar.class, fldchar); |
|---|
| 79 | |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | } |
|---|