C# 創建Word項目標號、多級項目編號列表

 

在Word文檔中,對於有多條並列的信息內容或者段落時,我們常以添加項目標號的形式來使文檔條理化,在於都是,文檔更具美觀性。另外,對於在邏輯上存在一定層級結構的內容時,也可以通過多級編號列表來標明文檔內容的層次,並且,在修改、編輯文檔時也增加了靈活性。因此,在本篇文檔中,將介紹如何在C#中通過使用類庫Free Spire.Doc for .NET 來創建項目編號列表和多級編號列表的方法。

 

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

 

使用方法:在安裝該類庫後,在項目中引用Spire.Doc.dll即可(dll文件可在安裝路徑下的Bin文件夾中獲取)

 

一、創建項目標號

 

 【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#】

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

 

實現效果:



 

 

以上代碼供參考,歡迎轉載(轉載請註明出處)。

 

感謝閱讀!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章