C#/VB.NET: Insert Lists in a Word Document
Lists are used in Word documents to outline, arrange, and emphasize text, making it easy for users to scan and understand a series of items. There are three different types of lists in Word, namely numbered lists, bulleted lists and multi-level lists. Numbered lists are used for items that have a sequence or priority, such as a series of instructions. Bulleted lists are used for items that have no particular priority, such as a list of functions or facts. Multi-level lists are used where there is a sequence and you want each paragraph numbered separately.
In this article, you will learn how to insert these types of lists into a Word document in C# and VB.NET using Spire.Doc for .NET.
- Insert a Numbered List in Word
- Insert a Bulleted List in Word
- Insert a Multi-Level Numbered List in Word
- Insert a Multi-Level Mixed-Type List in Word
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Insert a Numbered List in Word in C#, VB.NET
Spire.Doc for .NET offers the ListStyle class that you can use to create a numbered list style or a bulleted style. Then, the list style can be applied to a paragraph using Paragraph.ListFormat.ApplyStyle() method. The steps to create a numbered list are as follows.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the numbering type through ListLevel.PatternType property.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; namespace CreateOrderedList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a numbered list style ListStyle listStyle = new ListStyle(document, ListType.Numbered); listStyle.Name = "numberedList"; listStyle.Levels[0].PatternType = ListPatternType.DecimalEnclosedParen; listStyle.Levels[0].TextPosition = 20; document.ListStyles.Add(listStyle); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Required Web Development Skills:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph(); paragraph.AppendText("HTML"); paragraph.ListFormat.ApplyStyle("numberedList"); paragraph.ListFormat.ListLevelNumber = 0; //Add another four paragraphs and apply the numbered list style to them paragraph = section.AddParagraph(); paragraph.AppendText("CSS"); paragraph.ListFormat.ApplyStyle("numberedList"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("JavaScript"); paragraph.ListFormat.ApplyStyle("numberedList"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("Python"); paragraph.ListFormat.ApplyStyle("numberedList"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("MySQL"); paragraph.ListFormat.ApplyStyle("numberedList"); paragraph.ListFormat.ListLevelNumber = 0; //Save the document to file document.SaveToFile("NumberedList.docx", FileFormat.Docx); } } }
Insert a Bulleted List in Word in C#, VB.NET
The process of creating a bulleted list is similar to that of creating a numbered list. The difference is that when creating a list style, you must specify the list type as Bulleted and set a bullet symbol for it. The following are the detailed steps.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Bulleted.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the bullet symbol through ListLevel.BulletCharacter property.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; namespace CreateUnorderedList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a bulleted list style ListStyle listStyle = new ListStyle(document, ListType.Bulleted); listStyle.Name = "bulletedList"; listStyle.Levels[0].BulletCharacter = "\x00B7"; listStyle.Levels[0].CharacterFormat.FontName = "Symbol"; listStyle.Levels[0].TextPosition = 20; document.ListStyles.Add(listStyle); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Computer Science Subjects:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the bulleted list style to it paragraph = section.AddParagraph(); paragraph.AppendText("Data Structure"); paragraph.ListFormat.ApplyStyle("bulletedList"); paragraph.ListFormat.ListLevelNumber = 0; //Add another five paragraphs and apply the bulleted list style to them paragraph = section.AddParagraph(); paragraph.AppendText("Algorithm"); paragraph.ListFormat.ApplyStyle("bulletedList"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("Computer Networks"); paragraph.ListFormat.ApplyStyle("bulletedList"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("Operating System"); paragraph.ListFormat.ApplyStyle("bulletedList"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("C Programming"); paragraph.ListFormat.ApplyStyle("bulletedList"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("Theory of Computations"); paragraph.ListFormat.ApplyStyle("bulletedList"); paragraph.ListFormat.ListLevelNumber = 0; //Save the document to file document.SaveToFile("BulletedList.docx", FileFormat.Docx); } } }
Insert a Multi-Level Numbered List in Word in C#, VB.NET
A multi-level list consists of at least two different levels. Each level of a nested list is represented by the ListStyle.Levels[index] property, through which you can set the numbering type and prefix for a certain level. The following are the steps to create a multi-level numbered list in Word.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the numbering type and prefix.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; namespace CreateMultiLevelList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a numbered list style, specifying number prefix and pattern type of each level ListStyle listStyle = new ListStyle(document, ListType.Numbered); listStyle.Name = "levelstyle"; listStyle.Levels[0].PatternType = ListPatternType.Arabic; listStyle.Levels[0].TextPosition = 20; 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); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Here's a Multi-Level Numbered List:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph(); paragraph.AppendText("The first item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 0; //Add another five paragraphs and apply the numbered list stype to them paragraph = section.AddParagraph(); paragraph.AppendText("The second item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("The first sub-item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 1; paragraph = section.AddParagraph(); paragraph.AppendText("The second sub-item"); paragraph.ListFormat.ContinueListNumbering(); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph = section.AddParagraph(); paragraph.AppendText("A sub-sub-item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 2; paragraph = section.AddParagraph(); paragraph.AppendText("The third item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 0; //Save the document to file document.SaveToFile("MultilevelNumberedList.docx", FileFormat.Docx); } } }
Insert a Multi-Level Mixed-Type List in Word in C#, VB.NET
In some cases, you may want to mix number and symbol bullet points in a multi-level list. To create a mixed-type list, you just need to create a numbered list style and a bulleted list style and apply them to different paragraphs. The detailed steps are as follows.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create a numbered list style and a bulleted list style.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply different list style to different paragraphs using Paragraph.ListFormat.ApplyStyle() method.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; namespace CreateMultilevelMixedList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a numbered list style ListStyle numberedListStyle = new ListStyle(document, ListType.Numbered); numberedListStyle.Name = "numberedStyle"; numberedListStyle.Levels[0].PatternType = ListPatternType.Arabic; numberedListStyle.Levels[0].TextPosition = 20; numberedListStyle.Levels[1].PatternType = ListPatternType.LowLetter; document.ListStyles.Add(numberedListStyle); //Create a bulleted list style ListStyle bulletedListStyle = new ListStyle(document, ListType.Bulleted); bulletedListStyle.Name = "bulltedStyle"; bulletedListStyle.Levels[2].BulletCharacter = "\x002A"; bulletedListStyle.Levels[2].CharacterFormat.FontName = "Symbol"; document.ListStyles.Add(bulletedListStyle); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Here's a Multi-Level Mixed List:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph(); paragraph.AppendText("The first item"); paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph.ListFormat.ListLevelNumber = 0; //Add another five paragraphs and apply different list stype to them paragraph = section.AddParagraph(); paragraph.AppendText("The first sub-item"); paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph.ListFormat.ListLevelNumber = 1; paragraph = section.AddParagraph(); paragraph.AppendText("The second sub-item"); paragraph.ListFormat.ListLevelNumber = 1; paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph = section.AddParagraph(); paragraph.AppendText("The first sub-sub-item"); paragraph.ListFormat.ApplyStyle("bulltedStyle"); paragraph.ListFormat.ListLevelNumber = 2; paragraph = section.AddParagraph(); paragraph.AppendText("The second sub-sub-item"); paragraph.ListFormat.ApplyStyle("bulltedStyle"); paragraph.ListFormat.ListLevelNumber = 2; paragraph = section.AddParagraph(); paragraph.AppendText("The second item"); paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph.ListFormat.ListLevelNumber = 0; //Save the document to file document.SaveToFile("MultilevelMixedList.docx", FileFormat.Docx); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.