How to insert ordered and unordered lists into Word documents through C# code

Foreword:

When editing a Word document, using an ordered list or an unordered list can help us better organize the document content and make its logical relationship more intuitive and easy to understand. For example, create multiple subtitles in the article, or list multiple matters of the same type, etc. An ordered list arranges the contents in a specific order, while an unordered list has no specific order. Each item is preceded by a symbol or mark. Both of the above lists can be implemented through C# code. The following is an introduction to the method.

Preparation

In this tutorial, the class library used is Free Spire.Doc for .NET (free version), which supports processing Word documents on the .NET platform. There are two ways to install:

Method 1:

Search and install directly in Visual Studio.

  • First create a C# project in Visual Studio and open it.
  • Select “Solution Explorer” > right-click “References” > “Manage NuGet Packages”
  • Search for Free Spire.Doc for .NET and install it.

Method 2:

Manually introduce the Spire.doc.dll file into the program.

  • Download and install Free Spire.Doc for .NET.
  • Create a C# project in Visual Studio and open it.
  • Right-click “References” in “Solution Explorer”.
  • Select “Add Reference” > “Browse”, find the dll file in the BIN folder under the installation path, and click “OK”.

The second installation method is recommended, which makes it easier to find various reference codes in the installed “SampleCenter.exe”.

Reference code:

Ordered list

If you want to insert an ordered list into a Word document, you can create a Numbered list style through the ListStyle class provided by Free Spire.Doc for .NET. Then call the Paragraph.ListFormat.ApplyStyle() method to apply the created style to the specified paragraph. Below is the complete sample code.

using Spire.Doc;
using Spire.Doc.Documents;

namespace CreateOrderedList
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create Document object
            Document document = new Document();

            //Add 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 paragraph and set text
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("2022 Chinese City GDP Ranking");
            paragraph.Format.AfterSpacing = 5f;

            //Add other paragraphs and apply list styles
            paragraph = section.AddParagraph();
            paragraph.AppendText("Shanghai");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Beijing");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Shenzhen");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Chongqing");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Guangzhou");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            //Save the result document
            document.SaveToFile("Ordered List.docx", FileFormat.Docx);
        }
    }
}

The following is the rendering:

Unordered list

The process of inserting an unordered list is roughly similar to inserting an ordered list, but when creating a list style, it needs to be set to Bulleted. Then similarly, call the Paragraph.ListFormat.ApplyStyle() method to apply the style to the specified paragraph text.

using Spire.Doc;
using Spire.Doc.Documents;

namespace CreateUnorderedList
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create 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 paragraph and set text
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("2022 Chinese City GDP Ranking");
            paragraph.Format.AfterSpacing = 5f;

            //Add other paragraphs and apply list styles
            paragraph = section.AddParagraph();
            paragraph.AppendText("Shanghai");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Beijing");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Shenzhen");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Chongqing");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("Guangzhou");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            //Save the result document
            document.SaveToFile("Unordered list.docx", FileFormat.Docx);
        }
    }
}

The following is the rendering:

Reference article:

C#/VB.NET: Insert Lists in a Word Document