C # create a Word project label list and a multi-level Number list,
In Word documents, when there are multiple parallel information content or paragraphs, we often add project labels to make the documents organized and more intuitive. In addition, when there is a certain level of content in the logic, you can also use the multi-level serial number list to indicate the level of the document content, and also increase the flexibility in modifying and editing the document. Therefore, this document describes how to use the Class Library Free Spire. Doc for. NET in C # To create a project number list and a multi-level Number list.
Tools used:Free Spire. Doc for. NET (Community Edition)
Usage: After installing the class library, reference it in the projectSpire. Doc. dll(The dll file can be obtained from the Bin folder in the installation path)
1. Create a project label list
Using Spire.Doc;
Using Spire.Doc.Documents;
Namespace WordBullets
{
Class Program
{
Static void Main(string[] args)
{
/ / Initialize the Document class instance and add a section
Document doc = new Document();
Section section = doc.AddSection();
//Add seven paragraphs and add text separately
Paragraph para1 = section.AddParagraph();
para1.AppendText("International Political Organization");
Paragraph para2 = section.AddParagraph();
para2.AppendText("European Union (EU)");
Paragraph para3 = section.AddParagraph();
para3.AppendText("The Commonwealth of Independent States (CIS)");
Paragraph para4 = section.AddParagraph();
para4.AppendText("Shanghai Cooperation Organization");
Paragraph para5 = section.AddParagraph();
para5.AppendText("Arab Conference Alliance");
Paragraph para6 = section.AddParagraph();
para6.AppendText("International Ecological Security Cooperation Organization");
Paragraph para7 = section.AddParagraph();
para7.AppendText("Arab League");
/ / Create a paragraph format (font)
ParagraphStyle style = new ParagraphStyle(doc);
style.Name = "fontStyle";
style.CharacterFormat.FontName = "宋体";
style.CharacterFormat.FontSize = 12f;
doc.Styles.Add(style);
/ / Traverse all paragraphs
For (int i = 0; i < section.Paragraphs.Count; i++)
{
/ / Apply the bullet arrangement from the second paragraph
If (i != 0)
{
section.Paragraphs[i].ApplyStyle(BuiltinStyle.ListBullet2);
}
/ / Apply the font format to each paragraph
section.Paragraphs[i].ApplyStyle("fontStyle");
}
//Save and open the document
doc.SaveToFile("project list.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("project list.docx");
}
}
}
Test results:
2. Create a multi-level serial number list
Using Spire.Doc;
Using Spire.Doc.Documents;
Using Spire.Doc.Fields;
Namespace Multi_levelList_Doc
{
Class Program
{
Static void Main(string[] args)
{
/ / New Word document
Document doc = new Document();
Section section = doc.AddSection();
/ / Initialize the ListStyle object, specify the List type as a list of numbers and name
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 secondary list number prefix and mode
listStyle.Levels[1].NumberPrefix = "\x0000.";
listStyle.Levels[1].PatternType = ListPatternType.Arabic;
/ / Set the three-level list number prefix and mode
listStyle.Levels[2].NumberPrefix = "\x0000.\x0001.";
listStyle.Levels[2].PatternType = ListPatternType.Arabic;
/ / Add a new list style in the ListStyles collection
doc.ListStyles.Add(listStyle);
/ / Create a font format
Spire.Doc.Formatting.CharacterFormat format = new Spire.Doc.Formatting.CharacterFormat(doc);
format.FontName = "宋体";
/ / Add paragraphs, set the primary sequence
Paragraph paragraph = section.AddParagraph();
TextRange tr = paragraph.AppendText("main organization");
tr.ApplyCharacterFormat(format); //Apply font format
paragraph.ApplyStyle(BuiltinStyle.Heading1); //Apply title 1 style
paragraph.ListFormat.ApplyStyle("levelstyle"); //Apply list style
/ / Add paragraphs, set the primary sequence
Paragraph = section.AddParagraph();
Tr = paragraph.AppendText("main function");
tr.ApplyCharacterFormat(format);
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");
/ / Add paragraphs, set the secondary sequence
Paragraph = section.AddParagraph();
Tr = paragraph.AppendText("basic functions");
tr.ApplyCharacterFormat(format);
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ListLevelNumber = 1; //Set the level to the second level
paragraph.ListFormat.ApplyStyle("levelstyle");
/ / Add paragraphs, set the secondary sequence
Paragraph = section.AddParagraph();
Tr = paragraph.AppendText("5 major functions");
tr.ApplyCharacterFormat(format);
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle("levelstyle");
/ / Add paragraphs, set the three-level sequence
Paragraph = section.AddParagraph();
Tr = paragraph.AppendText("Management Functions \n Organization Functions \n Coordination Functions \n Regulation Functions \n Provide Functions");
tr.ApplyCharacterFormat(format);
paragraph.ApplyStyle(BuiltinStyle.Heading5);
paragraph.ListFormat.ListLevelNumber = 2; //Set the level to the third level
paragraph.ListFormat.ApplyStyle("levelstyle");
/ / Add paragraphs, set the primary sequence
Paragraph = section.AddParagraph();
Tr = paragraph.AppendText("Basic Principles");
tr.ApplyCharacterFormat(format);
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");
//Save and open the document
doc.SaveToFile("multilevel list.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Multilevel List.docx");
}
}
}
Test results:
The above code is for your reference. You are welcome to reprint it. (For more information, see the source code ).
Thank you for reading!