| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | package org.docx4j.samples; |
|---|
| 23 | |
|---|
| 24 | import java.math.BigInteger; |
|---|
| 25 | |
|---|
| 26 | import org.docx4j.XmlUtils; |
|---|
| 27 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 28 | import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart; |
|---|
| 29 | import org.docx4j.wml.Numbering; |
|---|
| 30 | import org.docx4j.wml.P; |
|---|
| 31 | import org.docx4j.wml.PPrBase.NumPr; |
|---|
| 32 | import org.docx4j.wml.PPrBase.NumPr.Ilvl; |
|---|
| 33 | import org.docx4j.wml.PPrBase.NumPr.NumId; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Creates a WordprocessingML document from scratch, |
|---|
| 37 | * including a numbering definitions part, and use |
|---|
| 38 | * it to demonstrate restart numbering. |
|---|
| 39 | * |
|---|
| 40 | * @author Jason Harrop |
|---|
| 41 | * @version 1.0 |
|---|
| 42 | */ |
|---|
| 43 | public class NumberingRestart { |
|---|
| 44 | |
|---|
| 45 | static org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); |
|---|
| 46 | |
|---|
| 47 | static String filename = "out-numberingrestart.docx"; |
|---|
| 48 | |
|---|
| 49 | public static void main(String[] args) throws Exception { |
|---|
| 50 | |
|---|
| 51 | System.out.println( "Creating package.."); |
|---|
| 52 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 53 | |
|---|
| 54 | // Add numbering part |
|---|
| 55 | NumberingDefinitionsPart ndp = new NumberingDefinitionsPart(); |
|---|
| 56 | wordMLPackage.getMainDocumentPart().addTargetPart(ndp); |
|---|
| 57 | ndp.setJaxbElement( (Numbering) XmlUtils.unmarshalString(initialNumbering) ); |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | // |
|---|
| 61 | wordMLPackage.getMainDocumentPart().addParagraphOfText("Example of restarting numbering"); |
|---|
| 62 | |
|---|
| 63 | P p = createNumberedParagraph(1, 0, "text on top level" ); |
|---|
| 64 | wordMLPackage.getMainDocumentPart().addObject(p); |
|---|
| 65 | |
|---|
| 66 | wordMLPackage.getMainDocumentPart().addObject( |
|---|
| 67 | createNumberedParagraph(1, 0, "more text on top level" )); |
|---|
| 68 | |
|---|
| 69 | wordMLPackage.getMainDocumentPart().addObject( |
|---|
| 70 | createNumberedParagraph(1, 1, "text on level 1" )); |
|---|
| 71 | |
|---|
| 72 | // Ok, lets restart numbering |
|---|
| 73 | long newNumId = ndp.restart(1, 0, 1); |
|---|
| 74 | |
|---|
| 75 | wordMLPackage.getMainDocumentPart().addObject( |
|---|
| 76 | createNumberedParagraph(newNumId, 0, "text on top level - restarted" )); |
|---|
| 77 | |
|---|
| 78 | // After first using newNumId, it doesn't matter whether |
|---|
| 79 | // subsequent paragraphs use that or the original numId |
|---|
| 80 | wordMLPackage.getMainDocumentPart().addObject( |
|---|
| 81 | createNumberedParagraph(newNumId, 0, "text on top level - using newNumId" )); |
|---|
| 82 | |
|---|
| 83 | wordMLPackage.getMainDocumentPart().addObject( |
|---|
| 84 | createNumberedParagraph(1, 0, "text on top level - using original NumId" )); |
|---|
| 85 | |
|---|
| 86 | // Now save it |
|---|
| 87 | wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/" + filename) ); |
|---|
| 88 | |
|---|
| 89 | System.out.println("Done. Saved " + filename); |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Create something like: |
|---|
| 95 | * |
|---|
| 96 | * <w:p> |
|---|
| 97 | <w:pPr> |
|---|
| 98 | <w:numPr> |
|---|
| 99 | <w:ilvl w:val="0"/> |
|---|
| 100 | <w:numId w:val="1"/> |
|---|
| 101 | </w:numPr> |
|---|
| 102 | </w:pPr> |
|---|
| 103 | <w:r> |
|---|
| 104 | <w:t>B</w:t> |
|---|
| 105 | </w:r> |
|---|
| 106 | </w:p> |
|---|
| 107 | |
|---|
| 108 | * @return |
|---|
| 109 | */ |
|---|
| 110 | private static P createNumberedParagraph(long numId, long ilvl, String paragraphText ) { |
|---|
| 111 | |
|---|
| 112 | P p = factory.createP(); |
|---|
| 113 | |
|---|
| 114 | org.docx4j.wml.Text t = factory.createText(); |
|---|
| 115 | t.setValue(paragraphText); |
|---|
| 116 | |
|---|
| 117 | org.docx4j.wml.R run = factory.createR(); |
|---|
| 118 | run.getRunContent().add(t); |
|---|
| 119 | |
|---|
| 120 | p.getParagraphContent().add(run); |
|---|
| 121 | |
|---|
| 122 | org.docx4j.wml.PPr ppr = factory.createPPr(); |
|---|
| 123 | p.setPPr( ppr ); |
|---|
| 124 | |
|---|
| 125 | // Create and add <w:numPr> |
|---|
| 126 | NumPr numPr = factory.createPPrBaseNumPr(); |
|---|
| 127 | ppr.setNumPr(numPr); |
|---|
| 128 | |
|---|
| 129 | // The <w:ilvl> element |
|---|
| 130 | Ilvl ilvlElement = factory.createPPrBaseNumPrIlvl(); |
|---|
| 131 | numPr.setIlvl(ilvlElement); |
|---|
| 132 | ilvlElement.setVal(BigInteger.valueOf(ilvl)); |
|---|
| 133 | |
|---|
| 134 | // The <w:numId> element |
|---|
| 135 | NumId numIdElement = factory.createPPrBaseNumPrNumId(); |
|---|
| 136 | numPr.setNumId(numIdElement); |
|---|
| 137 | numIdElement.setVal(BigInteger.valueOf(numId)); |
|---|
| 138 | |
|---|
| 139 | return p; |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | static final String initialNumbering = "<w:numbering xmlns:ve=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\">" |
|---|
| 145 | + "<w:abstractNum w:abstractNumId=\"0\">" |
|---|
| 146 | + "<w:nsid w:val=\"2DD860C0\"/>" |
|---|
| 147 | + "<w:multiLevelType w:val=\"multilevel\"/>" |
|---|
| 148 | + "<w:tmpl w:val=\"0409001D\"/>" |
|---|
| 149 | + "<w:lvl w:ilvl=\"0\">" |
|---|
| 150 | + "<w:start w:val=\"1\"/>" |
|---|
| 151 | + "<w:numFmt w:val=\"decimal\"/>" |
|---|
| 152 | + "<w:lvlText w:val=\"%1)\"/>" |
|---|
| 153 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 154 | + "<w:pPr>" |
|---|
| 155 | + "<w:ind w:left=\"360\" w:hanging=\"360\"/>" |
|---|
| 156 | + "</w:pPr>" |
|---|
| 157 | + "</w:lvl>" |
|---|
| 158 | + "<w:lvl w:ilvl=\"1\">" |
|---|
| 159 | + "<w:start w:val=\"1\"/>" |
|---|
| 160 | + "<w:numFmt w:val=\"lowerLetter\"/>" |
|---|
| 161 | + "<w:lvlText w:val=\"%2)\"/>" |
|---|
| 162 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 163 | + "<w:pPr>" |
|---|
| 164 | + "<w:ind w:left=\"720\" w:hanging=\"360\"/>" |
|---|
| 165 | + "</w:pPr>" |
|---|
| 166 | + "</w:lvl>" |
|---|
| 167 | + "<w:lvl w:ilvl=\"2\">" |
|---|
| 168 | + "<w:start w:val=\"1\"/>" |
|---|
| 169 | + "<w:numFmt w:val=\"lowerRoman\"/>" |
|---|
| 170 | + "<w:lvlText w:val=\"%3)\"/>" |
|---|
| 171 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 172 | + "<w:pPr>" |
|---|
| 173 | + "<w:ind w:left=\"1080\" w:hanging=\"360\"/>" |
|---|
| 174 | + "</w:pPr>" |
|---|
| 175 | + "</w:lvl>" |
|---|
| 176 | + "<w:lvl w:ilvl=\"3\">" |
|---|
| 177 | + "<w:start w:val=\"1\"/>" |
|---|
| 178 | + "<w:numFmt w:val=\"decimal\"/>" |
|---|
| 179 | + "<w:lvlText w:val=\"(%4)\"/>" |
|---|
| 180 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 181 | + "<w:pPr>" |
|---|
| 182 | + "<w:ind w:left=\"1440\" w:hanging=\"360\"/>" |
|---|
| 183 | + "</w:pPr>" |
|---|
| 184 | + "</w:lvl>" |
|---|
| 185 | + "<w:lvl w:ilvl=\"4\">" |
|---|
| 186 | + "<w:start w:val=\"1\"/>" |
|---|
| 187 | + "<w:numFmt w:val=\"lowerLetter\"/>" |
|---|
| 188 | + "<w:lvlText w:val=\"(%5)\"/>" |
|---|
| 189 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 190 | + "<w:pPr>" |
|---|
| 191 | + "<w:ind w:left=\"1800\" w:hanging=\"360\"/>" |
|---|
| 192 | + "</w:pPr>" |
|---|
| 193 | + "</w:lvl>" |
|---|
| 194 | + "<w:lvl w:ilvl=\"5\">" |
|---|
| 195 | + "<w:start w:val=\"1\"/>" |
|---|
| 196 | + "<w:numFmt w:val=\"lowerRoman\"/>" |
|---|
| 197 | + "<w:lvlText w:val=\"(%6)\"/>" |
|---|
| 198 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 199 | + "<w:pPr>" |
|---|
| 200 | + "<w:ind w:left=\"2160\" w:hanging=\"360\"/>" |
|---|
| 201 | + "</w:pPr>" |
|---|
| 202 | + "</w:lvl>" |
|---|
| 203 | + "<w:lvl w:ilvl=\"6\">" |
|---|
| 204 | + "<w:start w:val=\"1\"/>" |
|---|
| 205 | + "<w:numFmt w:val=\"decimal\"/>" |
|---|
| 206 | + "<w:lvlText w:val=\"%7.\"/>" |
|---|
| 207 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 208 | + "<w:pPr>" |
|---|
| 209 | + "<w:ind w:left=\"2520\" w:hanging=\"360\"/>" |
|---|
| 210 | + "</w:pPr>" |
|---|
| 211 | + "</w:lvl>" |
|---|
| 212 | + "<w:lvl w:ilvl=\"7\">" |
|---|
| 213 | + "<w:start w:val=\"1\"/>" |
|---|
| 214 | + "<w:numFmt w:val=\"lowerLetter\"/>" |
|---|
| 215 | + "<w:lvlText w:val=\"%8.\"/>" |
|---|
| 216 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 217 | + "<w:pPr>" |
|---|
| 218 | + "<w:ind w:left=\"2880\" w:hanging=\"360\"/>" |
|---|
| 219 | + "</w:pPr>" |
|---|
| 220 | + "</w:lvl>" |
|---|
| 221 | + "<w:lvl w:ilvl=\"8\">" |
|---|
| 222 | + "<w:start w:val=\"1\"/>" |
|---|
| 223 | + "<w:numFmt w:val=\"lowerRoman\"/>" |
|---|
| 224 | + "<w:lvlText w:val=\"%9.\"/>" |
|---|
| 225 | + "<w:lvlJc w:val=\"left\"/>" |
|---|
| 226 | + "<w:pPr>" |
|---|
| 227 | + "<w:ind w:left=\"3240\" w:hanging=\"360\"/>" |
|---|
| 228 | + "</w:pPr>" |
|---|
| 229 | + "</w:lvl>" |
|---|
| 230 | + "</w:abstractNum>" |
|---|
| 231 | + "<w:num w:numId=\"1\">" |
|---|
| 232 | + "<w:abstractNumId w:val=\"0\"/>" |
|---|
| 233 | + "</w:num>" |
|---|
| 234 | + "</w:numbering>"; |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | } |
|---|