How to Create Multi-level List Numbering in Word in C#, VB.NET

Multi-level list numbering allows you to format and organize text with nested numbers. The most common form of multi-level list is Heading Numbering, which summarizes the whole article with an outline list, frequently appearing in technical, academic and business documents. In this article, you’ll learn how to generate multi-level list numbering using Spire.Doc in C#, VB.NET.

Detailed Steps:

Step 1: Create a new Word document, add one section.

Document document = new Document();
Section section = document.AddSection();

Step 2: Define a new multi-level list style. Set its name as levelstyle, set ListType as Numbered, set parameters of level property. This list level operates independently of the style and controls the number formatting.

ListStyle listStyle = new ListStyle(document, ListType.Numbered);
listStyle.Name = "levelstyle";
listStyle.Levels[0].PatternType = ListPatternType.Arabic;
listStyle.Levels[1].NumberPrefix = "\x0000.";
listStyle.Levels[1].PatternType = ListPatternType.Arabic;
listStyle.Levels[2].NumberPrefix = "\x0000.\x0001.";
listStyle.Levels[2].PatternType = ListPatternType.Arabic;
document.ListStyles.Add(listStyle);

Step 3: Create a paragraph, append text to paragraph and apply the paragraph style with Heading1. Apply the defined list style ‘levelstyle’ to this paragraph.

Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

Step 4: Repeat step 3 to add more paragraphs to this document. Here, you need to set different ListLevelNumber as 1 or 2 to make sure the sub-paragraph and sub-sub-paragraph will be assigned to deferent list templates.

paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ListLevelNumber = 1;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("A sub-sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading5);
paragraph.ListFormat.ListLevelNumber = 2;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The third item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

Step 5: Save the file.

document.SaveToFile(@"result.docx", FileFormat.Docx);

Result:

How to Create Multi-level List Numbering in Word in C#, VB.NET

Full Code:

[C#]
Document document = new Document();
Section section = document.AddSection();

ListStyle listStyle = new ListStyle(document, ListType.Numbered);
listStyle.Name = "levelstyle";
listStyle.Levels[0].PatternType = ListPatternType.Arabic;
listStyle.Levels[1].NumberPrefix = "\x0000.";
listStyle.Levels[1].PatternType = ListPatternType.Arabic;
listStyle.Levels[2].NumberPrefix = "\x0000.\x0001.";
listStyle.Levels[2].PatternType = ListPatternType.Arabic;
document.ListStyles.Add(listStyle);

Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ListLevelNumber = 1;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("A sub-sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading5);
paragraph.ListFormat.ListLevelNumber = 2;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The third item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

document.SaveToFile(@"result.docx", FileFormat.Docx);
[VB.NET]
Dim document As New Document()
Dim section As Section = document.AddSection()

Dim listStyle As New ListStyle(document, ListType.Numbered)
listStyle.Name = "levelstyle"
listStyle.Levels(0).PatternType = ListPatternType.Arabic
listStyle.Levels(1).NumberPrefix = vbNullChar & "."
listStyle.Levels(1).PatternType = ListPatternType.Arabic
listStyle.Levels(2).NumberPrefix = vbNullChar & "." & ChrW(1) & "."
listStyle.Levels(2).PatternType = ListPatternType.Arabic
document.ListStyles.Add(listStyle)

Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("The first item")
paragraph.ApplyStyle(BuiltinStyle.Heading1)
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The second item")
paragraph.ApplyStyle(BuiltinStyle.Heading1)
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The first sub-item")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
paragraph.ListFormat.ListLevelNumber = 1
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The second sub-item")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
paragraph.ListFormat.ContinueListNumbering()
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("A sub-sub-item")
paragraph.ApplyStyle(BuiltinStyle.Heading5)
paragraph.ListFormat.ListLevelNumber = 2
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The third item")
paragraph.ApplyStyle(BuiltinStyle.Heading1)
paragraph.ListFormat.ApplyStyle("levelstyle")

document.SaveToFile("result.docx", FileFormat.Docx)