How to: Create Numbered List in Code
- 2 minutes to read
To create a numbered list in code, perform the following steps.
- Add a new AbstractNumberingList instance to the Document.AbstractNumberingLists collection.
- Specify a NumberingListBase.NumberingType property, so that it is equal to the NumberingType.MultiLevel value.
- For each list level, specify the paragraph characteristics using the ListLevel.ParagraphProperties property. Set the left indentation so that each level has a different offset from the left. Specify a hanging first line and set the first line indent value to provide enough space for a number that precedes the text.
- Set the ListLevelProperties.Start value that is the number from which to start the list.
- Specify a format used to display a number via the ListLevelProperties.DisplayFormatString property.
- Use the Add method of the collection of lists in the document (accessible via the Document.NumberingLists property) to add a list definition to the document. The parameter is the index of an abstract numbering list created previously. The Add method creates a pattern that can be applied to a paragraph so that the paragraph looks like a list item.
- To include a paragraph in a list, set the Paragraph.ListIndex property of a paragraph to the index of a list in the document and specify the Paragraph.ListLevel property.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/word-processing-document-api-examples-t418535.
Document document = server.Document;
document.BeginUpdate();
// Numbered list with roman format
AbstractNumberingList abstractListNumberingRoman = document.AbstractNumberingLists.Add();
abstractListNumberingRoman.NumberingType = NumberingType.Simple;
ListLevel level = abstractListNumberingRoman.Levels[0];
level.ParagraphProperties.LeftIndent = 150;
level.ParagraphProperties.FirstLineIndentType = ParagraphFirstLineIndent.Hanging;
level.ParagraphProperties.FirstLineIndent = 75;
level.Start = 1;
level.NumberingFormat = NumberingFormat.UpperRoman;
level.DisplayFormatString = "{0}.";
NumberingList numberingList = document.NumberingLists.Add(0);
document.EndUpdate();
document.AppendText("Line 1\nLine 2\nLine 3");
// Convert all paragraphs to list items.
document.BeginUpdate();
ParagraphCollection paragraphs = document.Paragraphs;
paragraphs.AddParagraphsToList(document.Range, numberingList, 0);
document.EndUpdate();