Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Feb 12, 2024 6:08 pm

Hello, I wonder what is the easiest way to create such result (shown on attached image). Specifically, how to create such numbered list and add number of the list's level to caption?

Adamantin
 
Posts: 3
Joined: Tue Dec 06, 2022 2:09 pm

Tue Feb 13, 2024 6:24 am

Hello,

Thank you for your inquiry.
Based on your description, please refer to the code below to fulfill your requirement.
Code: Select all
//Initialize a document
Document document = new Document();
//Add a section
Section sec = document.AddSection();
//Add paragraph and set list style
Paragraph paragraph = sec.AddParagraph();

//Create list style
ListStyle numberList = new ListStyle(document, ListType.Numbered);
numberList.Name = "numberList";
numberList.Levels[0].CharacterFormat.TextColor = System.Drawing.Color.FromArgb(255, 68, 114, 196);
numberList.Levels[1].NumberPrefix = "\x0000.";
numberList.Levels[1].PatternType = ListPatternType.Arabic;
numberList.Levels[1].CharacterFormat.TextColor = System.Drawing.Color.FromArgb(255, 68, 114, 196);
numberList.Levels[2].NumberPrefix = "\x0000.\x0001.";
numberList.Levels[2].PatternType = ListPatternType.Arabic;
numberList.Levels[2].CharacterFormat.TextColor = System.Drawing.Color.FromArgb(255, 68, 114, 196);
document.ListStyles.Add(numberList);

//Add paragraph and apply the list style
paragraph.AppendText("Level 1");
paragraph.ListFormat.ApplyStyle(numberList.Name);

paragraph = sec.AddParagraph();
paragraph.AppendText("Level 2");
paragraph.ListFormat.ApplyStyle(numberList.Name);
paragraph.ListFormat.ListLevelNumber = 1;

paragraph = sec.AddParagraph();
paragraph.AppendText("Level 3");
paragraph.ListFormat.ApplyStyle(numberList.Name);
paragraph.ListFormat.ListLevelNumber =2;

for (int i = 0; i < sec.Paragraphs.Count; i++)
{
    Paragraph para = sec.Paragraphs[i];
    para.Format.LineSpacing = 18;
    for (int j = 0; j < para.ChildObjects.Count;j++)
    {
        DocumentObject documentObject = para.ChildObjects[j];
        if (documentObject.DocumentObjectType.Equals(DocumentObjectType.TextRange))
        {
            TextRange range = (TextRange)documentObject;
            range.CharacterFormat.FontSize = 12;
            range.CharacterFormat.TextColor = System.Drawing.Color.FromArgb(255,68,114,196);
        }
    }
}

document.SaveToFile("result.docx", FileFormat.Docx2019);

If you have any further questions or need assistance with anything else, please don't hesitate to let me know.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1652
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc