Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Manage Lists

    • 11 minutes to read

    The Word Processing Document API supports bulleted, numbered, and multilevel lists. The following objects specify list settings:

    Property Object Description
    WordDocument.NumberingLists NumberingList All lists in the document.
    NumberingList.Template ListTemplate Appearance settings for a list.
    ListTemplate.Levels ListLevel Nine list levels. Each level specifies its number format, number text, start value, indents, and font settings.

    The following properties turn paragraphs into list items:

    The following diagram shows the list structure:

    WordDocument
    ├── NumberingLists: NumberingList
    │   └── Template (ListTemplate)
    │       └── Levels: ListLevel (indexes 0–8)
    ├── ParagraphStyles: ParagraphStyle
    │   ├── NumberingList
    │   └── ListLevelIndex
    └── Body
        └── Paragraph
            ├── NumberingList
            └── ListLevelIndex
    

    Create a Numbered or Bulleted List

    Call the following methods to create a template for a numbered, bulleted, or multilevel list:

    Pass the template to the NumberingListCollection.Add(template) method. The Add method creates a list, adds the list to the WordDocument.NumberingLists collection, and returns the created NumberingList object.

    To add a paragraph to a list, assign the list to Paragraph.NumberingList property. The Paragraph.ListLevelIndex property specifies the list level. The value range is from 0 to 8. If the ListLevelIndex property is null, the paragraph is on the first level.

    If the WordDocument.NumberingLists collection does not contain the assigned list, the API adds the list to the collection. The API also adds the list when you add a paragraph with a list to a document (for example, a paragraph from another document).

    The following code snippet creates a numbered list and a bulleted list with a nested item:

    DevExpress Word Processing Document API - Numbered and Bulleted Lists in a Document

    using DevExpress.Docs.Word;
    
    using WordDocument document = new WordDocument();
    
    // Create a numbered list.
    NumberingList steps = document.NumberingLists.Add(ListTemplate.CreateNumbered());
    document.Body.AddParagraph("Open the project.").NumberingList = steps;
    document.Body.AddParagraph("Build the solution.").NumberingList = steps;
    document.Body.AddParagraph("Run the tests.").NumberingList = steps;
    
    // Create a bulleted list.
    NumberingList requirements = document.NumberingLists.Add(ListTemplate.CreateBulleted());
    document.Body.AddParagraph(".NET Framework 4.6.2 or later").NumberingList = requirements;
    document.Body.AddParagraph(".NET 10").NumberingList = requirements;
    
    Paragraph platforms = document.Body.AddParagraph("Windows, Linux, and macOS");
    platforms.NumberingList = requirements;
    platforms.ListLevelIndex = 1;
    

    Create a Multilevel List

    A multilevel list displays parent level numbers in each item number (for example, 1., 1.1., and 1.1.1.).

    Create a list from the template that the ListTemplate.CreateMultilevel() method returns and set the Paragraph.ListLevelIndex property of each paragraph to the item level.

    The following code snippet creates a multilevel list with three levels:

    DevExpress Word Processing Document API - Multilevel List in a Document

    NumberingList outline = document.NumberingLists.Add(ListTemplate.CreateMultilevel());
    
    (string Text, int Level)[] items = {
        ("Introduction", 0),
        ("Purpose", 1),
        ("Scope", 1),
        ("Supported formats", 2),
        ("Installation", 0),
        ("System requirements", 1)
    };
    
    foreach (var item in items) {
        Paragraph paragraph = document.Body.AddParagraph(item.Text);
        paragraph.NumberingList = outline;
        paragraph.ListLevelIndex = item.Level;
    }
    

    Customize List Levels

    To specify custom list level settings, create a ListTemplate object and modify items in the ListTemplate.Levels collection. You can also modify levels of a predefined template.

    Use the following ListLevel properties to customize a list level:

    Property Description
    Format Gets or sets the number style for list items for the list level (for example, decimal numbers, lowercase letters, or bullets).
    DisplayFormat Gets or sets the number text pattern for list items at the list level (for example, %1. or Step %1:).
    Start Gets or sets the start number for list items at the list level.
    Alignment Gets or sets item number alignment for the list level.
    Separator Gets or sets the character that separates the item number from the item text.
    RestartAfterLevel Gets or sets the index of the previous list level that restarts numbering on the level.
    LegalNumbering Gets or sets whether the level displays numbers of previous levels as Arabic numerals.
    ParagraphStyleName Gets or sets the name of the paragraph style linked to the list level.
    ParagraphProperties Gets paragraph formatting settings for list items at the list level.
    CharacterProperties Gets character formatting settings for item numbers and bullets for the list level.

    The DisplayFormat property specifies the number text pattern. The %1 to %9 placeholders insert current numbers of list levels (for example, the Step %1: pattern displays Step 1:, Step 2:, and so on).

    To create a bullet level, set the Format property to NumberingFormat.Bullet and the DisplayFormat property to the bullet character. Use the CharacterProperties.Font property of the ListLevel.CharacterProperties object to specify the bullet font.

    Use ParagraphProperties.LeftIndent and ParagraphProperties.FirstLineIndent properties of the ListLevel.ParagraphProperties object to position level items. A negative FirstLineIndent value specifies a hanging indent for the item number. Indents are in document units (1/300 of an inch).

    The following code snippet creates a template with bold custom number text on the first level and lowercase letters with custom indents on the second level:

    DevExpress Word Processing Document API - List with Custom Levels in a Document

    ListTemplate template = new ListTemplate();
    
    ListLevel stepLevel = template.Levels[0];
    stepLevel.DisplayFormat = "Step %1:";
    stepLevel.Separator = ListNumberSeparator.Space;
    stepLevel.CharacterProperties.Bold = true;
    
    ListLevel optionLevel = template.Levels[1];
    optionLevel.Format = NumberingFormat.LowerLetter;
    optionLevel.DisplayFormat = "%2)";
    optionLevel.ParagraphProperties.LeftIndent = 300;
    optionLevel.ParagraphProperties.FirstLineIndent = -75;
    
    NumberingList list = document.NumberingLists.Add(template);
    document.Body.AddParagraph("Select a deployment target.").NumberingList = list;
    
    Paragraph choice = document.Body.AddParagraph("A local server");
    choice.NumberingList = list;
    choice.ListLevelIndex = 1;
    
    choice = document.Body.AddParagraph("A cloud service");
    choice.NumberingList = list;
    choice.ListLevelIndex = 1;
    
    document.Body.AddParagraph("Publish the application.").NumberingList = list;
    

    Modify Levels of an Existing List

    Use the NumberingList.Template property to access a list template (for example, the template of a list in a loaded document). In a loaded document, lists that share a template refer to the same ListTemplate object.

    To customize levels of a single list, call the ListTemplate.Clone() method. Create a list from the template copy and assign the new list to the Paragraph.NumberingList property of the list items. The new list starts a separate numbering sequence.

    The following code snippet finds the first list item in the document body and encloses first-level numbers in parentheses in all lists that share the item’s list template:

    foreach (Block block in document.Body) {
        if (block is Paragraph paragraph && paragraph.NumberingList != null) {
            ListTemplate template = paragraph.NumberingList.Template;
            template.Levels[0].DisplayFormat = "(%1)";
            break;
        }
    }
    

    Restart Numbering

    Paragraphs that refer to the same NumberingList object form a single numbering sequence. Numbering continues even if other paragraphs separate list items.

    If multiple lists use the same template, numbering continues from one list to the next. A list with another template starts a separate numbering sequence.

    To restart numbering for a list, call the NumberingList.SetStartOverride(levelIndex, value) method. The specified number takes precedence over the ListLevel.Start value of the template level only for this list. To clear the start number override, pass null as the value parameter.

    The NumberingList.GetStartOverride(levelIndex) method returns the start number override for the specified level.

    The following code snippet creates two lists from the same template and restarts numbering in the second list:

    ListTemplate template = ListTemplate.CreateNumbered();
    
    NumberingList setupSteps = document.NumberingLists.Add(template);
    document.Body.AddParagraph("Install the package.").NumberingList = setupSteps;
    document.Body.AddParagraph("Register the license.").NumberingList = setupSteps;
    
    document.Body.AddParagraph("Deploy the application:");
    
    NumberingList deploymentSteps = document.NumberingLists.Add(template);
    // Restart numbering at 1. Otherwise, the second list continues numbering with 3.
    deploymentSteps.SetStartOverride(0, 1);
    document.Body.AddParagraph("Publish the project.").NumberingList = deploymentSteps;
    document.Body.AddParagraph("Copy files to the server.").NumberingList = deploymentSteps;
    

    Number Paragraphs with Paragraph Styles

    A paragraph style can include list numbering (for example, for numbered headings). Use ParagraphStyle.NumberingList and ParagraphStyle.ListLevelIndex properties to specify the list and level for paragraphs that use the style.

    A style based on another style (StyleBase.ParentName) inherits the list from the parent style. Set the ListLevelIndex property of the child style to place paragraphs that use the child style on another level.

    The paragraph’s own list takes precedence over the style list. For a paragraph numbered by a style, the Paragraph.NumberingList property gets null. If you set the Paragraph.ListLevelIndex property for such a paragraph, the paragraph moves to the specified level of the style list.

    To exclude a paragraph from style numbering, set the Paragraph.NumberingDisabled property to true.

    The following code snippet creates Heading 1 and Heading 2 paragraph styles with multilevel numbering, applies the styles to paragraphs, and disables numbering for one paragraph:

    DevExpress Word Processing Document API - Headings Numbered by Paragraph Styles

    NumberingList headingList = document.NumberingLists.Add(ListTemplate.CreateMultilevel());
    
    ParagraphStyle heading1 = document.ParagraphStyles.Add("Heading 1");
    heading1.NumberingList = headingList;
    heading1.ListLevelIndex = 0;
    heading1.CharacterProperties.Bold = true;
    
    ParagraphStyle heading2 = document.ParagraphStyles.Add("Heading 2");
    heading2.ParentName = heading1.Name;
    heading2.ListLevelIndex = 1;
    
    document.Body.AddParagraph("Overview").StyleName = heading1.Name;
    document.Body.AddParagraph("Key Features").StyleName = heading2.Name;
    document.Body.AddParagraph("System Requirements").StyleName = heading2.Name;
    
    Paragraph notes = document.Body.AddParagraph("Release Notes");
    notes.StyleName = heading2.Name;
    notes.NumberingDisabled = true;
    
    document.Body.AddParagraph("Getting Started").StyleName = heading1.Name;
    

    Refer to the following help topic for additional information: Apply Styles to Document Content.

    Access List Items

    When you load a document, the NumberingLists collection contains document lists. To find list items, iterate through paragraphs and check the Paragraph.NumberingList property.

    Paragraphs numbered by a paragraph style do not refer to a list directly. To check whether a paragraph style includes a list, use the ParagraphStyle.NumberingList property (for example, document.ParagraphStyles[paragraph.StyleName].NumberingList).

    The DocumentFragment.GetText(range) method returns paragraph text that includes the item number (or bullet character) and the paragraph mark.

    The following code snippet loads a document, writes the number of lists to the console, and writes the level, number format, and text of each list item in the document body:

    using System;
    using System.IO;
    using DevExpress.Docs.Word;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    DocumentFragment body = document.Body;
    
    Console.WriteLine($"Lists: {document.NumberingLists.Count}");
    foreach (Block block in body) {
        if (block is Paragraph paragraph && paragraph.NumberingList != null) {
            int levelIndex = paragraph.ListLevelIndex ?? 0;
            ListLevel level = paragraph.NumberingList.Template.Levels[levelIndex];
            string text = body.GetText(body.GetRange(paragraph)).TrimEnd();
            Console.WriteLine($"Level {levelIndex} ({level.Format}): {text}");
        }
    }
    

    Remove Numbering and Lists

    To remove a paragraph from a list, set the Paragraph.NumberingList property to null. If the paragraph style includes a list, the paragraph gets numbering from the style. To disable all numbering for the paragraph, set the Paragraph.NumberingDisabled property to true.

    NumberingList and NumberingDisabled properties are automatically synchronized. If you assign a list to the NumberingList property, the API sets the NumberingDisabled property to false. If you set the NumberingDisabled property to true, the API sets the NumberingList property to null.

    The following code snippet disables numbering for all paragraphs in the document body:

    foreach (Block block in document.Body) {
        if (block is Paragraph paragraph)
            paragraph.NumberingDisabled = true;
    }
    

    To remove a list from the document, call the NumberingListCollection.Remove(list) or NumberingListCollection.RemoveAt(index) method. The NumberingListCollection.Clear() method removes all lists.

    Paragraphs and paragraph styles that use a removed list lose numbering but keep the link to the list (the NumberingList property returns the removed list). To restore numbering, add the list to the NumberingLists collection again.

    Refer to the following help topic for additional information: Manage Paragraphs.