C# wpf创建多级列表文档(11)

1,新建一个wpf项目,添加一个button按钮,在button事件里面写如下代码:

//新建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");

        }

然后测试,程序生成一个word文档
C# wpf创建多级列表文档(11)_第1张图片
成功。

你可能感兴趣的:(wpf)