Create multi-level list numbering in word document

  • NPOI
  • Spire.Doc
  • Download Sample Code

using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create document
            XWPFDocument doc = new XWPFDocument();

            //Create numbering
            XWPFNumbering numbering = doc.CreateNumbering();
            string abstractNumId = numbering.AddAbstractNum();
            string numId = numbering.AddNum(abstractNumId);            

            //Create paragragh and set its list level
            XWPFParagraph para1 = doc.CreateParagraph();
            XWPFRun run1 = para1.CreateRun();
            run1.SetText("The first paragraph");
            para1.SetNumID(numId, "0");

            //Create paragragh and set the list level
            XWPFParagraph para2 = doc.CreateParagraph();
            XWPFRun run2 = para2.CreateRun();
            run2.SetText("The second paragraph");
            para2.SetNumID(numId, "0");

            //Create paragragh and apply multi level list 
            XWPFParagraph para3 = doc.CreateParagraph();
            XWPFRun run3 = para3.CreateRun();
            run3.SetText("The third paragraph");
            para3.SetNumID(numId, "0");
            para3 = doc.CreateParagraph();
            run3 = para3.CreateRun();
            run3.SetText("The first sub-item");
            para3.SetNumID(numId, "1");
            para3 = doc.CreateParagraph();
            run3 = para3.CreateRun();
            run3.SetText("The second sub-item");
            para3.SetNumID(numId, "1");
            para3 = doc.CreateParagraph();
            run3 = para3.CreateRun();
            run3.SetText("The sub-sub-item");
            para3.SetNumID(numId, "2");

            //Save the file and Launch
            using (FileStream sw = new FileStream("ListNumbering.doc", FileMode.Create))
            {
                doc.Write(sw);
            }
            System.Diagnostics.Process.Start("ListNumbering.doc");
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Spire.Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create document
            Document document = new Document();
            Section section = document.AddSection();

            //Define a new multi-level list style
            ListStyle listStyle = new ListStyle(document, ListType.Bulleted);
            listStyle.Name = "levelstyle";
            listStyle.Levels[0].PatternType = ListPatternType.Bullet;
            listStyle.Levels[1].PatternType = ListPatternType.Bullet;
            listStyle.Levels[2].PatternType = ListPatternType.Bullet;
            document.ListStyles.Add(listStyle);

            //Create paragraph and apply the defined list style
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("The first paragraph");
            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Create paragraph and apply the defined list style
            paragraph = section.AddParagraph();
            paragraph.AppendText("The second paragraph");
            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Create paragragh and apply multi level list by setting different ListLevelNumber 
            paragraph = section.AddParagraph();
            paragraph.AppendText("The third paragraph");
            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ListFormat.ApplyStyle("levelstyle");

            paragraph = section.AddParagraph();
            paragraph.AppendText("The first sub-item");
            paragraph.ApplyStyle(BuiltinStyle.Heading2);
            paragraph.ListFormat.ListLevelNumber = 1;
            paragraph.ListFormat.ApplyStyle("levelstyle");

            paragraph = section.AddParagraph();
            paragraph.AppendText("The second sub-item");
            paragraph.ApplyStyle(BuiltinStyle.Heading2);
            paragraph.ListFormat.ContinueListNumbering();
            paragraph.ListFormat.ApplyStyle("levelstyle");

            paragraph = section.AddParagraph();
            paragraph.AppendText("The sub-sub-item");
            paragraph.ApplyStyle(BuiltinStyle.Heading5);
            paragraph.ListFormat.ListLevelNumber = 2;
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Save and Launch
            document.SaveToFile("ListNumbering.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("ListNumbering.docx");
        }
    }
}