Page 1 of 1

Samples for unordered and ordered list

PostPosted: Tue Mar 06, 2012 4:24 am
by cheopz
Hello,

I couldn't find any examples for creating an unordered or ordered list.
Are there any in the doc4j samples? If not could someone give me a short code snippet for an unordered list?

Thx!

Re: Samples for unordered and ordered list

PostPosted: Tue Mar 06, 2012 9:28 am
by jason
You need a numbering part; you could Google "docx4j numbering". But you should start by creating a docx in Word which contains what you want, then unzip it and have a look to see your target structure.

Having said all that, the NumberingRestart sample will help :-)

Re: Samples for unordered and ordered list

PostPosted: Wed Mar 07, 2012 3:49 am
by cheopz
Hi,

thx for the answer!

Had a look at the sample and unzipped the docx to have a look at document.xml and numbering.xml .
But couldn't get it work to create a list like :

• One
• Two
• Three

Re: Samples for unordered and ordered list

PostPosted: Fri Mar 09, 2012 5:54 am
by cheopz
Hi,

my problem is that i need to replace some placeholder with a text, table, graphic and bulleted list.
I figured out to replace the placeholder with paragraphs containing the first three items,
but don't how to replace with a bulleted list.
Questions:
- The list consists of <w:p> elements?
- Elements have special pStyle?
- Do i need to define a NumberingDefinitionsPart?

A short example would be really helpful.

Re: Samples for unordered and ordered list

PostPosted: Fri Mar 09, 2012 5:04 pm
by jason
list items are paragraphs. one paragraph per list item.

you don't need a style, but you do need numPr and the numbering definitions part.

fyi, the OpenDoPE approach to replacing content is designed to make it easy to repeat things (for example, list items) - you'd just put the list item in a content control of type repeat, and specify the content to be retrieved from your XML data file using an XPath expression

Re: Samples for unordered and ordered list

PostPosted: Mon Mar 19, 2012 10:50 pm
by cheopz
I created a docx with a simple list which i want to create, to have an idea how it should look like.

The document.xml looks like that (excuse as i use a german version of ms word):

Code: Select all
-<w:p w:rsidP="00EF2E07" w:rsidRDefault="00B27589" w:rsidR="00042101">
  -<w:pPr>
       <w:pStyle w:val="Listenabsatz"/>
      -<w:numPr>
           <w:ilvl w:val="0"/>
           <w:numId w:val="5"/>
      </w:numPr>
   </w:pPr>
  -<w:r>
       <w:t>o</w:t>
   </w:r>
   <w:bookmarkStart w:name="_GoBack" w:id="0"/>
   <w:bookmarkEnd w:id="0"/>
  -<w:r>
         <w:t>ne</w:t>
   </w:r></w:p>
  -<w:p w:rsidP="00EF2E07" w:rsidRDefault="00B27589" w:rsidR="00EF2E07">
     -<w:pPr>
         <w:pStyle w:val="Listenabsatz"/>
        -<w:numPr>
             <w:ilvl w:val="0"/>
             <w:numId w:val="5"/>
         </w:numPr>
       </w:pPr> 
      -<w:r>
           <w:t>two</w:t>
       </w:r>
      -<w:r w:rsidR="00EF2E07">
         <w:t xml:space="preserve"> </w:t>
       </w:r>
     </w:p>
    -<w:p w:rsidP="00EF2E07" w:rsidRDefault="00B27589" w:rsidR="00EF2E07" w:rsidRPr="00EF2E07">
        -<w:pPr>
             <w:pStyle w:val="Listenabsatz"/>
            -<w:numPr>
                   <w:ilvl w:val="0"/>
                   <w:numId w:val="5"/>
             </w:numPr>
          </w:pPr>
         -<w:r>
              <w:t>three</w:t>
          </w:r>
      </w:p>


So my list should look something like this. But my generated docx looks like this:

Code: Select all
<w:p w:rsidRDefault="00280775" w:rsidP="00280775" w:rsidR="00280775">
   -<w:r>
       -<w:p>
            -<w:pPr>
                -<w:numPr>
                     <w:ilvl w:val="0"/>
                     <w:numId w:val="1"/>
                 </w:numPr>
             </w:pPr>
            -<w:r>
                <w:t>One</w:t>
             </w:r>
        </w:p>
       -<w:p>
            -<w:pPr>
               -<w:numPr>
                    <w:ilvl w:val="0"/>
                    <w:numId w:val="1"/>
                </w:numPr>
             </w:pPr>
            -<w:r>
                <w:t>Two</w:t>
             </w:r>
         </w:p>
        -<w:p>
            -<w:pPr>
                -<w:numPr>
                    <w:ilvl w:val="0"/>
                    <w:numId w:val="1"/>
                 </w:numPr>
             </w:pPr>
            -<w:r>
               <w:t>Three</w:t>
             </w:r>
         </w:p>
     </w:r>
</w:p>


The problem is the space between the paragraphs and also the numbering for the following elements in the document is overwritten so the next
heading after the list is also bulleted.

Here an excerpt of my code:
Code: Select all
public static P createUnnumberedList(List<String> list, int i) throws Docx4JException, JAXBException {
          ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
          P  p = factory.createP();
                                       
          org.docx4j.wml.Text  t = factory.createText();
          t.setValue(list.get(i));

          org.docx4j.wml.R  run = factory.createR();
          run.getContent().add(t);
         
          p.getContent().add(run);
         
          org.docx4j.wml.PPr ppr = factory.createPPr();
         
          p.setPPr( ppr );
         
          // Create and add <w:numPr>
          NumPr numPr =  factory.createPPrBaseNumPr();
          ppr.setNumPr(numPr);

         
          // The <w:ilvl> element
          Ilvl ilvlElement = factory.createPPrBaseNumPrIlvl();
          numPr.setIlvl(ilvlElement);
          ilvlElement.setVal(BigInteger.valueOf(0));
         
          // The <w:numId> element
          NumId numIdElement = factory.createPPrBaseNumPrNumId();
          numPr.setNumId(numIdElement);
          numIdElement.setVal(BigInteger.valueOf(1));
         
          return p;
    }

Code: Select all
static final String initialNumbering = "<w:numbering xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" " +
         "xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" " +
         "xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" " +
         "xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" " +
         "xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" " +
         "xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
         "xmlns:w10=\"urn:schemas-microsoft-com:office:word\" " +
         "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\">" +
         "xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" " +
         "xmlns:v=\"urn:schemas-microsoft-com:vml\" " +
         "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" " +
         "xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
         "xmlns:o=\"urn:schemas-microsoft-com:office:office\" " +
         "xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" " +
         "xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\" "
          + "<w:abstractNum w:abstractNumId=\"0\">"
          + "<w:nsid w:val=\"09A23B4A\"/>"
          + "<w:multiLevelType w:val=\"multilevel\"/>"
          + "<w:tmpl w:val=\"04070025\"/>"
          + "<w:lvl w:ilvl=\"0\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift1\"/>"
              + "<w:lvlText w:val=\"%1\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"432\" w:hanging=\"432\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"1\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift2\"/>"
              + "<w:lvlText w:val=\"%1.%2\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"576\" w:hanging=\"576\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"2\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift3\"/>"
              + "<w:lvlText w:val=\"%1.%2.%3\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"720\" w:hanging=\"720\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"3\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift4\"/>"
              + "<w:lvlText w:val=\"%1.%2.%3.%4\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"864\" w:hanging=\"864\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"4\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift5\"/>"
              + "<w:lvlText w:val=\"%1.%2.%3.%4.%5\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"1008\" w:hanging=\"1008\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"5\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift6\"/>"
              + "<w:lvlText w:val=\"%1.%2.%3.%4.%5.%6\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"1152\" w:hanging=\"1152\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"6\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift7\"/>"
              + "<w:lvlText w:val=\"%1.%2.%3.%4.%5.%6.%7\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"1296\" w:hanging=\"1296\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"7\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift8\"/>"
              + "<w:lvlText w:val=\"%1.%2.%3.%4.%5.%6.%7.%8\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"1440\" w:hanging=\"1440\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
          + "<w:lvl w:ilvl=\"8\">"
              + "<w:start w:val=\"1\"/>"
              + "<w:numFmt w:val=\"decimal\"/>"
              + "<w:pStyle w:val=\"berschrift9\"/>"
              + "<w:lvlText w:val=\"%1.%2.%3.%4.%5.%6.%7.%8.%9\"/>"
              + "<w:lvlJc w:val=\"left\"/>"
              + "<w:pPr>"
                  + "<w:ind w:left=\"1584\" w:hanging=\"1584\"/>"
              + "</w:pPr>"
          + "</w:lvl>"
      + "</w:abstractNum>"
      + "<w:abstractNum w:abstractNumId=\"1\">"
       + "<w:nsid w:val=\"41DA0569\"/>"
       + "<w:multiLevelType w:val=\"hybridMultilevel\"/>"
       + "<w:tmpl w:val=\"53DED020\"/>"
       + "<w:lvl w:ilvl=\"0\" w:tplc=\"04070001\">"
       +   "<w:start w:val=\"1\"/>"
        +    "<w:numFmt w:val=\"bullet\"/>"
        +   "<w:lvlText w:val=\"\"/>"
        +   "<w:lvlJc w:val=\"left\"/>"
        +    "<w:pPr>"
        +       "<w:ind w:hanging=\"360\" w:left=\"720\"/>"
        +    "</w:pPr>"
       +   "<w:rPr>"
        +       "<w:rFonts w:hint=\"default\" w:hAnsi=\"Symbol\" w:ascii=\"Symbol\"/>"
        +    "</w:rPr>"
        + "</w:lvl>"
        + "</w:abstractNum>"
      + "<w:num w:numId=\"1\">"
          + "<w:abstractNumId w:val=\"1\"/>"
      + "</w:num>"
      + "<w:num w:numId=\"2\">"
          + "<w:abstractNumId w:val=\"0\"/>"
       + "</w:num>"
       + "<w:num w:numId=\"3\">"
       +    "<w:abstractNumId w:val=\"0\"/>"
       +   "<w:lvlOverride w:ilvl=\"0\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"1\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"2\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"3\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"4\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"5\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"6\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"7\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       +   "<w:lvlOverride w:ilvl=\"8\">"
       +      "<w:startOverride w:val=\"1\"/>"
       +   "</w:lvlOverride>"
       + "<w:num w:numId=\"4\">"
          + "<w:abstractNumId w:val=\"2\"/>"
       + "</w:num>"       
       + "</w:num>"
   + "</w:numbering>";

Re: Samples for unordered and ordered list

PostPosted: Tue Mar 20, 2012 2:28 pm
by jason
cheopz wrote:The problem is the space between the paragraphs


I guess you had the property for this (PPrBase.Spacing) set in your paragraph style Listenabsatz; you'll need to set before/after to whatever you want (have a look at Listenabsatz in styles.xml in your sample docx).

cheopz wrote:and also the numbering for the following elements in the document is overwritten so the next
heading after the list is also bulleted.


I guess they are also using list definition numId 0. You'll need to define and use separate list numbering definitions to avoid this.

Re: Samples for unordered and ordered list

PostPosted: Thu Oct 05, 2017 5:44 am
by dwilches
For those with the same issue, what worked for me is this:


NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
ndp.setJaxbElement(ndp.unmarshalDefaultNumbering());
mdp.addTargetPart(ndp));

Then for numbered lists you set `nId` to 1, and for bulleted you set `nId` to 2.