Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

DocX – Add nested lists to Microsoft Word document programmatically

DocX-MicrosoftWord

If you had one of those days looking for solution on how to easily add a nested list to Microsoft Word document by using C# search no more. Below you can find an example on how to do that using DocX package.

Prerequisites

Following prerequisites are required to create Microsoft Word document programmatically:

DocX package
Visual Studio 2013 / Visual Studio 2015 (Free Community Edition will do)
Basic understanding on how to code in C# (C Sharp)
Basic tutorial on how to start coding with DocX package can be found on our blog post DocX – A short tutorial for beginners HelloWorld()

Example code

Following code provides basic example on how to add Nested Lists to Microsoft Word programmatically.

private static void AddList()
{
    Console.WriteLine("\tAddList()");

    using (var document = DocX.Create(@"docs\Lists.docx"))
    {
        var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered);
                //Add a numbered list starting at 2
        document.AddListItem(numberedList, "Second List Item.");
        document.AddListItem(numberedList, "Third list item.");
        document.AddListItem(numberedList, "First sub list item", 1);

        document.AddListItem(numberedList, "Nested item.", 2);
        document.AddListItem(numberedList, "Fourth nested item.");

        var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
        document.AddListItem(bulletedList, "Second bullet item");
        document.AddListItem(bulletedList, "Sub bullet item", 1);
        document.AddListItem(bulletedList, "Second sub bullet item", 2);
        document.AddListItem(bulletedList, "Third bullet item");

        document.InsertList(numberedList);
        document.InsertList(bulletedList);
        document.Save();
        Console.WriteLine("\tCreated: docs\\Lists.docx");
    }
}

Example Output

Code above should create an output file called Lists.docx. If you start it up the view should be showing nice Nested Lists.

Example Notes

Above example is part of DocX Examples and can be found in DocX source package under Examples \ Program.cs. This example uses RelativeDirectory.cs class. It's required for the code sample to work and are included in the sources of DocX.

Keep that in mind when working with the code to either download the DocX package or change the code appropriately. Below you can find Relative Directory class just in case you have no access to sources.

using System;
using System.IO;

namespace Examples
{
    class RelativeDirectory
    {
        // Author D. Bolton see https://cplus.about.com (c) 2010
        private DirectoryInfo _dirInfo;

        public string Dir
        {
            get
            {
                return _dirInfo.Name;
            }
        }

        public string Path
        {
            get { return _dirInfo.FullName; }
            set
            {
                try
                {
                    DirectoryInfo newDir = new DirectoryInfo(value);
                    _dirInfo = newDir;
                }
                catch
                {
                    // silent
                }
            }
        }
        public RelativeDirectory()
        {
            _dirInfo = new DirectoryInfo(Environment.CurrentDirectory);
        }

        public RelativeDirectory(string absoluteDir)
        {
            _dirInfo = new DirectoryInfo(absoluteDir);
        }

        public Boolean Up(int numLevels)
        {
            for (int i = 0; i < numLevels; i++)
            {
                DirectoryInfo tempDir = _dirInfo.Parent;
                if (tempDir != null)
                    _dirInfo = tempDir;
                else
                    return false;
            }
            return true;
        }

        public Boolean Up()
        {
            return Up(1);
        }

        public Boolean Down(string match)
        {
            DirectoryInfo[] dirs = _dirInfo.GetDirectories(match + '*');
            _dirInfo = dirs[0];
            return true;
        }

    }

}

Related Posts

Leave a comment

You must be logged in to post a comment.