设置二级序列 paragraph = section.AddParagraph();tr = paragraph.App

时间:2021-09-04 02:59:00

在Word文档中,,对付有多条并列的信息内容或者段落时,我们常以添加项方针号的形式来使文档层次化,在阅读时,文档也更具美不雅观性。此外,对付在逻辑上存在必然层级布局的内容时,也可以通过多级编号列表来标明文档内容的条理,并且,在改削、编纂文档时也增加了灵活性。因此,在本篇文档中,将介绍如安在C#中通过使用类库Free Spire.Doc for .NET 来创建项目编号列表和多级编号列表的要领。

使用工具Free Spire.Doc for .NET(社区版)

使用要领:在安置该类库后,在项目中引用Spire.Doc.dll即可(dll文件可在安置路径下的Bin文件夹中获取)

一、创建项方针号列表

using Spire.Doc; using Spire.Doc.Documents; namespace WordBullets { class Program { static void Main(string[] args) { //初始化Document类实例,并添加section Document doc = new Document(); Section section = doc.AddSection(); //添加七个段落并分袂添加文字 Paragraph para1 = section.AddParagraph(); para1.AppendText("国际政治类组织"); Paragraph para2 = section.AddParagraph(); para2.AppendText("欧洲联盟(欧盟)"); Paragraph para3 = section.AddParagraph(); para3.AppendText("自主国家联合体(独联体)"); Paragraph para4 = section.AddParagraph(); para4.AppendText("上海合作组织"); Paragraph para5 = section.AddParagraph(); para5.AppendText("阿拉伯会议联盟"); Paragraph para6 = section.AddParagraph(); para6.AppendText("国际生态安适合作组织"); Paragraph para7 = section.AddParagraph(); para7.AppendText("阿拉伯国家联盟"); //创建段落格局(字体) ParagraphStyle style = new ParagraphStyle(doc); style.Name = "fontStyle"; style.CharacterFormat.FontName = "宋体"; style.CharacterFormat.FontSize = 12f; doc.Styles.Add(style); //遍历所有段落 for (int i = 0; i < section.Paragraphs.Count; i++) { //从第二段开始应用项目标记摆列 if (i != 0) { section.Paragraphs[i].ApplyStyle(BuiltinStyle.ListBullet2); } //应用字体格局到每一段 section.Paragraphs[i].ApplyStyle("fontStyle"); } //生存并打开文档 doc.SaveToFile("项目列表.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("项目列表.docx"); } } }

测试效果:

设置二级序列 paragraph = section.AddParagraph();tr = paragraph.App

二、创建多级编号列表

using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Multi_levelList_Doc { class Program { static void Main(string[] args) { //新建Word文档 Document doc = new Document(); Section section = doc.AddSection(); //初始化ListStyle东西,指定List类型为数字列表并定名 ListStyle listStyle = new ListStyle(doc, ListType.Numbered); listStyle.Name = "levelstyle"; //设定一级列表模式为阿拉伯数字 listStyle.Levels[0].PatternType = ListPatternType.Arabic; //设置二级列表数字前缀及模式 listStyle.Levels[1].NumberPrefix = "\x0000."; listStyle.Levels[1].PatternType = ListPatternType.Arabic; //设置三级列表数字前缀及模式 listStyle.Levels[2].NumberPrefix = "\x0000.\x0001."; listStyle.Levels[2].PatternType = ListPatternType.Arabic; //在ListStyles调集中添加新建的list style doc.ListStyles.Add(listStyle); //创建字体格局 Spire.Doc.Formatting.CharacterFormat format = new Spire.Doc.Formatting.CharacterFormat(doc); format.FontName = "宋体"; //添加段落,设置一级序列 Paragraph paragraph = section.AddParagraph(); TextRange tr = paragraph.AppendText("主要组织机构"); tr.ApplyCharacterFormat(format); //应用字体格局 paragraph.ApplyStyle(BuiltinStyle.Heading1); //应用标题1样式 paragraph.ListFormat.ApplyStyle("levelstyle"); //应用列表样式 //添加段落,设置一级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("主要本能机能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading1); paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置二级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("根基本能机能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading2); paragraph.ListFormat.ListLevelNumber = 1; //设置品级为第二品级 paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置二级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("5大本能机能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading2); paragraph.ListFormat.ContinueListNumbering(); paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置三级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("打点本能机能 \n 组织本能机能 \n 协调本能机能 \n 调治本能机能 \n 供给本能机能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading5); paragraph.ListFormat.ListLevelNumber = 2; //设置品级为第三品级 paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置一级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("基来源根底则"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading1); paragraph.ListFormat.ApplyStyle("levelstyle"); //生存并打开文档 doc.SaveToFile("多级列表.docx", FileFormat.Docx); System.Diagnostics.Process.Start("多级列表.docx"); } } }

测试效果:

设置二级序列 paragraph = section.AddParagraph();tr = paragraph.App