在Word文档中,对于有多条并列的信息内容或者段落时,我们常以添加项目标号的形式来使文档条理化,在阅读时,文档也更具美观性。另外,对于在逻辑上存在一定层级结构的内容时,也可以通过多级编号列表来标明文档内容的层次,并且,在修改、编辑文档时也增加了灵活性。因此,在本篇文档中,将介绍如何在C#中通过使用类库Free Spire.Doc for .NET 来创建项目编号列表和多级编号列表的方法。
使用工具: Free Spire.Doc for .NET(社区版)
使用方法:在安装该类库后,在项目中引用@H_502_13@Spire.Doc.dll即可(dll文件可在安装路径下的Bin文件夹中获取)
一、创建项目标号列表using Spire.Doc; 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 = 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(); } 保存并打开文档 doc.Savetofile(项目列表.docx,fileFormat.Docx2013); System.Diagnostics.Process.Start(); } }}
测试效果:
二、创建多级编号列表Spire.Doc.documents; Spire.Doc.FIElds; Multi_levelList_Doc{ 新建Word文档 document doc = 初始化ListStyle对象,指定List类型为数字列表并命名 ListStyle ListStyle = ListStyle(doc,ListType.Numbered); ListStyle.name = levelstyle; 设定一级列表模式为阿拉伯数字 ListStyle.Levels[0].PatternType = ListPatternType.arabic; 设置二级列表数字前缀及模式 ListStyle.Levels[1].NumberPrefix = \x0000.; ListStyle.Levels[1].PatternType =设置三级列表数字前缀及模式 ListStyle.Levels[2].NumberPrefix = \x0000.\x0001.2].PatternType =在ListStyles集合中添加新建的List style doc.ListStyles.Add(ListStyle); 创建字体格式 Spire.Doc.Formatting.CharacterFormat format = 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("); 应用列表样式 添加段落,设置一级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText(主要职能); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.heading1); paragraph.ListFormat.ApplyStyle(添加段落,设置二级序列 paragraph =基本职能); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.heading2); paragraph.ListFormat.ListLevelNumber = 1; 设置等级为第二等级 paragraph.ListFormat.ApplyStyle(5大职能); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.heading2); paragraph.ListFormat.ContinueListNumbering(); paragraph.ListFormat.ApplyStyle(添加段落,设置三级序列 paragraph =管理职能 \n 组织职能 \n 协调职能 \n 调节职能 \n 提供职能); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.heading5); paragraph.ListFormat.ListLevelNumber = 2; 设置等级为第三等级 paragraph.ListFormat.ApplyStyle(基本原则多级列表.docx); } }}
测试效果:
以上代码供参考,欢迎转载(转载请注明出处)。
感谢阅读!
总结以上是内存溢出为你收集整理的C# 创建Word项目标号列表、多级编号列表全部内容,希望文章能够帮你解决C# 创建Word项目标号列表、多级编号列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)