Search code examples
ms-wordopenxmlwordprocessingml

creating two bulleted lists in Word 2007 by OpenXML


I want to get output two bulleted lists like on the picture:

enter image description here

But what I get is both the lists have the second (black circle) skin.

My code:

private static void AddNumberingDefinition(WordprocessingDocument docx)
        {
            NumberingDefinitionsPart numberingPart =
  docx.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>();

            Numbering element =
              new Numbering(
                //     new Nsid(){ Val="FFFFFF80"},
                new AbstractNum(new Nsid() { Val = "FFFFFF80" },
                    new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
                new TemplateCode() { Val = "4970B4E6" },
                  new Level(
                    new NumberingFormat() { Val = NumberFormatValues.Bullet },
                    new LevelText() { Val = "°" },//char.ConvertFromUtf32(61616) }
                    new LevelJustification() { Val = LevelJustificationValues.Left }//,
                //  new RunProperties() { RunFonts = new RunFonts() { Hint = FontTypeHintValues.Default, Ascii = "Symbol", 
//HighAnsi = "Symbol" } }
                  ) { LevelIndex = 0 }
                ) { AbstractNumberId = 0 },
                new NumberingInstance(
                  new AbstractNumId() { Val = 0 }
                ) { NumberID = 1 },

           // element.Save(numberingPart);

         //   element = new Numbering(
            new AbstractNum(new Nsid() { Val = "FFFFFF89" },
                new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
                new TemplateCode() { Val = "4970B4E6" },
                  new Level(
                    new NumberingFormat() { Val = NumberFormatValues.Bullet },
                    new LevelText() { Val = "•" },//char.ConvertFromUtf32(61616) }
                    new LevelJustification() { Val = LevelJustificationValues.Left }//,
                //    new RunProperties() { RunFonts = new RunFonts() { Hint = FontTypeHintValues.Default, Ascii = "Symbol",
// HighAnsi = "Symbol" } }
                  ) { LevelIndex = 0 }
                ) { AbstractNumberId = 1 },
                new NumberingInstance(
                  new AbstractNumId() { Val = 1 }
                ) { NumberID = 2 });

            element.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            element.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            element.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            element.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            element.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            element.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            element.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            element.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            element.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");

            element.Save(numberingPart);
        }

        private static Paragraph GenerateParagraph()
        {
            return new Paragraph(
           new ParagraphProperties(
             new NumberingProperties(
               new NumberingLevelReference() { Val = 0 },
               new NumberingId() { Val = 1 })),
           new Run(
             new RunProperties(),
             new Text("Hello, Wordl!"))
             );
        }

        private static Paragraph GenerateParagraph2()
        {
            return new Paragraph(
           new ParagraphProperties(
             new NumberingProperties(
               new NumberingLevelReference() { Val = 0 },
               new NumberingId() { Val = 2 })),
           new Run(
             new RunProperties(),
             new Text("Hello, Wordl!"))
             );
        }

I don't know, where is the problem hidden? I used the Productivity Tool for Open XML to compare my generated file and one created manually but I can't get it to work. Thanks in advance for any hints.


Solution

  • After a few hours I found that, the order of inserting the AbstractNum and NumberingInstance objects is important..

    Open XML SDK 2.0 Productivity Tools has a Validate method which showed me the validate error.

    Working code:

            private static void AddNumberingDefinition(WordprocessingDocument docx)
            {
                NumberingDefinitionsPart numberingPart =
      docx.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>();
    
                var a = new AbstractNum(new Nsid() { Val = "FFFFFF80" },
                      new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
                  new TemplateCode() { Val = "4970B4E6" },
                    new Level(
                      new NumberingFormat() { Val = NumberFormatValues.Bullet },
                      new LevelText() { Val = "°" },
                      new LevelJustification() { Val = LevelJustificationValues.Left }
                    ) { LevelIndex = 0 }
                  ) { AbstractNumberId = 0 };
    
                var a1 = new NumberingInstance(
                     new AbstractNumId() { Val = 0 }
                   ) { NumberID = 1 };
    
                var b = new AbstractNum(new Nsid() { Val = "FFFFFF89" },
                      new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
                      new TemplateCode() { Val = "4970B4E6" },
                        new Level(
                          new NumberingFormat() { Val = NumberFormatValues.Bullet },
                          new LevelText() { Val = "•" },
                          new LevelJustification() { Val = LevelJustificationValues.Left }
                        ) { LevelIndex = 0 }
                      ) { AbstractNumberId = 1 };
                var b1 = new NumberingInstance(
                     new AbstractNumId() { Val = 1 }
                   ) { NumberID = 2 };
    
                Numbering element = new Numbering();
    
                element.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
                element.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
                element.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
                element.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
                element.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
                element.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
                element.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
                element.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                element.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
    
                element.Append(a);
                element.Append(b);
                element.Append(a1);
                element.Append(b1);
                element.Save(numberingPart);
            }