C#创建Word项目标号列表、多级编号列表-创新互联

在Word文档中,对于有多条并列的信息内容或者段落时,我们常以添加项目标号的形式来使文档条理化,在阅读时,文档也更具美观性。另外,对于在逻辑上存在一定层级结构的内容时,也可以通过多级编号列表来标明文档内容的层次,并且,在修改、编辑文档时也增加了灵活性。因此,在本篇文档中,将介绍如何在C#中通过使用类库Free Spire.Doc for .NET 来创建项目编号列表和多级编号列表的方法。
使用工具: Free Spire.Doc for .NET(社区版)
使用方法:在安装该类库后,在项目中引用Spire.Doc.dll即可(dll文件可在安装路径下的Bin文件夹中获取)

专注于为中小企业提供成都网站制作、成都做网站、外贸营销网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业合水免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

一、 创建项目标号列表

C#

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");
        }
    }
}

测试结果:
C# 创建Word项目标号列表、多级编号列表

二、 创建多级编号列表

C#

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");
        }
    }
}

测试结果:
C# 创建Word项目标号列表、多级编号列表

以上代码供参考,欢迎转载(转载请注明出处)。
感谢阅读!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站标题:C#创建Word项目标号列表、多级编号列表-创新互联
链接地址:https://www.cdcxhl.com/article44/dijjhe.html

成都网站建设公司_创新互联,为您提供品牌网站建设企业网站制作网站设计公司网站制作微信小程序移动网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设