C ා create Word item label list and multi-level number list

Keywords: C#

In a Word document, when there are multiple parallel information contents or paragraphs, we often add item labels to make the document organized. When reading, the document is also more beautiful. In addition, for content with a certain logical hierarchy, the hierarchy of document content can also be indicated by multi-level number list, and the flexibility is increased when modifying and editing documents. Therefore, in this document, I will show you how to create a project number list and a multi-level number list in C ා by using the class library free spirit.doc for. Net.

Use tools: Free flame.doc for. Net (Community Edition)

Usage: after installing the class library, you can reference Spire.Doc.dll in the project (the DLL file can be obtained in the Bin folder under the installation path)

1, Create project label list

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

namespace WordBullets
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialization Document Class and add section
            Document doc = new Document();
            Section section = doc.AddSection();

            //Add seven paragraphs and text separately
            Paragraph para1 = section.AddParagraph();
            para1.AppendText("International political organizations");
            Paragraph para2 = section.AddParagraph();
            para2.AppendText("European Union (EU)");
            Paragraph para3 = section.AddParagraph();
            para3.AppendText("Commonwealth of Independent States (CIS)");
            Paragraph para4 = section.AddParagraph();
            para4.AppendText("Shanghai Cooperation Organization ");
            Paragraph para5 = section.AddParagraph();
            para5.AppendText("League of Arab conferences");
            Paragraph para6 = section.AddParagraph();
            para6.AppendText("International Organization for ecological security cooperation");
            Paragraph para7 = section.AddParagraph();
            para7.AppendText("arab league");

            //Create paragraph format (font)
            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "fontStyle";
            style.CharacterFormat.FontName = "Song style";
            style.CharacterFormat.FontSize = 12f;
            doc.Styles.Add(style);

            //Traverse all paragraphs
            for (int i = 0; i < section.Paragraphs.Count; i++)
            {
                //Apply bullet arrangement from the second paragraph
                if (i != 0)
                {
                    section.Paragraphs[i].ApplyStyle(BuiltinStyle.ListBullet2);
                }

                //Apply font formatting to each paragraph
                section.Paragraphs[i].ApplyStyle("fontStyle");

            }
            //Save and open document
            doc.SaveToFile("List of items.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("List of items.docx");
        }
    }
}

Test effect:

2, Create multi level number list

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

namespace Multi_levelList_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Newly build Word File
            Document doc = new Document();
            Section section = doc.AddSection();

            //Initialization ListStyle Objects, specifying List Type is number list and named
            ListStyle listStyle = new ListStyle(doc, ListType.Numbered);
            listStyle.Name = "levelstyle";

            //Set the first level list mode to Arabic numerals
            listStyle.Levels[0].PatternType = ListPatternType.Arabic;

            //Set the number prefix and mode of the secondary list
            listStyle.Levels[1].NumberPrefix = "\x0000.";
            listStyle.Levels[1].PatternType = ListPatternType.Arabic;

            //Set the number prefix and mode of the three-level list
            listStyle.Levels[2].NumberPrefix = "\x0000.\x0001.";
            listStyle.Levels[2].PatternType = ListPatternType.Arabic;

            //stay ListStyles Add new in collection list style
            doc.ListStyles.Add(listStyle);

            //Create font format
            Spire.Doc.Formatting.CharacterFormat format = new Spire.Doc.Formatting.CharacterFormat(doc);
            format.FontName = "Song style";

            //Add paragraph, set first level sequence
            Paragraph paragraph = section.AddParagraph();
            TextRange tr = paragraph.AppendText("Main organization");
            tr.ApplyCharacterFormat(format); //Apply font formatting
            paragraph.ApplyStyle(BuiltinStyle.Heading1); //Apply Heading 1 style
            paragraph.ListFormat.ApplyStyle("levelstyle"); //Apply list style

            //Add paragraph, set first level sequence
            paragraph = section.AddParagraph();
            tr = paragraph.AppendText("Main functions");
            tr.ApplyCharacterFormat(format);
            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Add paragraph, set secondary sequence
            paragraph = section.AddParagraph();
            tr = paragraph.AppendText("basic function");
            tr.ApplyCharacterFormat(format);
            paragraph.ApplyStyle(BuiltinStyle.Heading2);
            paragraph.ListFormat.ListLevelNumber = 1; //Set level 2
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Add paragraph, set secondary sequence
            paragraph = section.AddParagraph();
            tr = paragraph.AppendText("5 Great function");
            tr.ApplyCharacterFormat(format);
            paragraph.ApplyStyle(BuiltinStyle.Heading2);
            paragraph.ListFormat.ContinueListNumbering();
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Add paragraph, set three-level sequence
            paragraph = section.AddParagraph();
            tr = paragraph.AppendText("management functions \n Organization function \n Coordination function \n Regulating function \n Provide functions");
            tr.ApplyCharacterFormat(format);
            paragraph.ApplyStyle(BuiltinStyle.Heading5);
            paragraph.ListFormat.ListLevelNumber = 2; //Set level 3
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Add paragraph, set first level sequence
            paragraph = section.AddParagraph();
            tr = paragraph.AppendText("Basic principle");
            tr.ApplyCharacterFormat(format);
            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ListFormat.ApplyStyle("levelstyle");

            //Save and open document
            doc.SaveToFile("Multilevel list.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Multilevel list.docx");
        }
    }
}

Test effect:

The above code is for reference, please reprint (reprint please indicate the source).

Thank you for reading!  

Posted by doood on Sat, 04 Apr 2020 23:37:58 -0700