Page 1 of 1

Nested ordered lists

PostPosted: Thu Oct 03, 2013 11:59 pm
by zzzimon
Hi! I wanto create the following nested list:

1. Apple
   1. Orange
   2. Pear
2. Banana

But when I do it with the bottom code the numbers 1 and 2 in the nested list infront of Orange and Pear disappear. Any clues?

Code: Select all
   public static void main(String[] args) throws Exception {
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
      NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();

      String str =
      "<w:numbering xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"+         
         "<w:abstractNum w:abstractNumId=\"0\">"+
            "<w:nsid w:val=\"369E7F02\"/>"+
            "<w:multiLevelType w:val=\"hybridMultilevel\"/>"+
            "<w:tmpl w:val=\"89B427B2\"/>"+
            "<w:lvl w:ilvl=\"0\" w:tplc=\"041D000F\">"+
               "<w:start w:val=\"1\"/>"+
               "<w:numFmt w:val=\"decimal\"/>"+
               "<w:lvlText w:val=\"%1.\"/>"+
               "<w:lvlJc w:val=\"left\"/>"+
               "<w:pPr>"+
                  "<w:ind w:hanging=\""+300+"\" w:left=\""+300+"\"/>"+
               "</w:pPr>"+
            "</w:lvl>"+
            "<w:lvl w:ilvl=\"1\" w:tplc=\"041D000F\">"+
               "<w:start w:val=\"1\"/>"+
               "<w:numFmt w:val=\"decimal\"/>"+
               "<w:lvlText w:val=\"%1.\"/>"+
               "<w:lvlJc w:val=\"left\"/>"+
               "<w:pPr>"+
                  "<w:ind w:hanging=\""+600+"\" w:left=\""+600+"\"/>"+
               "</w:pPr>"+
            "</w:lvl>"+
         "</w:abstractNum>"+   
         "<w:num w:numId=\"1\">"+
            "<w:abstractNumId w:val=\"0\"/>"+
         "</w:num>"+   
         "<w:num w:numId=\"2\">"+
            "<w:abstractNumId w:val=\"0\"/>"+
         "</w:num>"+            
      "</w:numbering>";
      
      ndp.unmarshal(new ByteArrayInputStream(str.getBytes()));
      
      mdp.addTargetPart(ndp);
      
      long list1NumId = ndp.restart(1,0,1);      
      
      str =
      "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:rsidP=\"00FF661E\" w:rsidRDefault=\"00FF661E\" w:rsidR=\"00000000\">"+
         "<w:pPr>"+
            "<w:pStyle w:val=\"ListParagraph\"/>"+
            "<w:numPr>"+
               "<w:ilvl w:val=\"0\"/>"+
               "<w:numId w:val=\""+list1NumId+"\"/>"+
            "</w:numPr>"+
         "</w:pPr>"+
         "<w:r>"+
            "<w:t>test1</w:t>"+
         "</w:r>"+
      "</w:p>";
      mdp.addObject(org.docx4j.XmlUtils.unmarshalString(str) );

      long list2NumId = ndp.restart(2,1,1);
      
      str =
      "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:rsidP=\"00FF661E\" w:rsidRDefault=\"00FF661E\" w:rsidR=\"00000000\">"+
         "<w:pPr>"+
            "<w:pStyle w:val=\"ListParagraph\"/>"+
            "<w:numPr>"+
               "<w:ilvl w:val=\"1\"/>"+
               "<w:numId w:val=\""+list2NumId+"\"/>"+
            "</w:numPr>"+
         "</w:pPr>"+
         "<w:r>"+
            "<w:t>test2</w:t>"+
         "</w:r>"+
      "</w:p>";
      mdp.addObject(org.docx4j.XmlUtils.unmarshalString(str) );
            
      str =
      "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:rsidP=\"00FF661E\" w:rsidRDefault=\"00FF661E\" w:rsidR=\"00000000\">"+
         "<w:pPr>"+
            "<w:pStyle w:val=\"ListParagraph\"/>"+
            "<w:numPr>"+
               "<w:ilvl w:val=\"1\"/>"+
               "<w:numId w:val=\""+list2NumId+"\"/>"+
            "</w:numPr>"+
         "</w:pPr>"+
         "<w:r>"+
            "<w:t>test3</w:t>"+
         "</w:r>"+
      "</w:p>";
      mdp.addObject(org.docx4j.XmlUtils.unmarshalString(str) );

      str =
      "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:rsidP=\"00FF661E\" w:rsidRDefault=\"00FF661E\" w:rsidR=\"00000000\">"+
         "<w:pPr>"+
            "<w:pStyle w:val=\"ListParagraph\"/>"+
            "<w:numPr>"+
               "<w:ilvl w:val=\"0\"/>"+
               "<w:numId w:val=\""+list1NumId+"\"/>"+
            "</w:numPr>"+
         "</w:pPr>"+
         "<w:r>"+
            "<w:t>test4</w:t>"+
         "</w:r>"+
      "</w:p>";
      mdp.addObject(org.docx4j.XmlUtils.unmarshalString(str) );      
      
      wordMLPackage.save(new java.io.File("wordfile.docx") );
      
   }

Re: Nested ordered lists

PostPosted: Mon Oct 07, 2013 12:35 pm
by jason
When I run your code, I see:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
1.      test1
1.        test2
1.        test3 <-------------------------------
2.      test4
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


I have previously found that Word won't honour an override, unless ilvl 0 is used in the docx. You are probably running into that or something similar.

If the list you want is as simple as you describe, you don't need to restart numbering at all. Just use your numId=1, and ilvl 0,1,1,0 respectively. numId 2 is not required