Word控件Spire.Doc 【段落处理】教程(八):如何在 C#、VB.NET 中的 Word 中创建多级列表编号

翻译|使用教程|编辑:胡涛|2022-05-13 15:08:30.293|阅读 365 次

概述:在本文中,您将学习如何在 C#、VB.NET 中使用 Spire.Doc 生成多级列表编号。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

相关链接:

多级列表编号允许您使用嵌套编号格式化和组织文本。多级列表最常见的形式是标题编号,它用大纲列表概括整篇文章,经常出现在技术、学术和商业文档中。在本文中,您将学习如何在 C#、VB.NET 中使用 Spire.Doc 生成多级列表编号。

Spire.Doc for.NET 最新下载

详细步骤:

第 1 步 : 创建一个新的 Word 文档,添加一个部分。

Document document = new Document();
Section section = document.AddSection();

第 2 步定义新的多级列表样式。设置其名称为levelstyle,设置ListType 为Numbered,设置level 属性的参数。此列表级别独立于样式运行并控制数字格式。

ListStyle listStyle = new ListStyle(document, 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;
document.ListStyles.Add(listStyle);

第 3 步创建一个段落,将文本附加到段落并使用 Heading1 应用段落样式。将定义的列表样式“levelstyle”应用于此段落。

Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

第 4 步重复第 3 步以向该文档添加更多段落。在这里,您需要将不同的 ListLevelNumber 设置为 1 或 2,以确保将子段落和子段落分配给不同的列表模板。

paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ListLevelNumber = 1;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("A sub-sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading5);
paragraph.ListFormat.ListLevelNumber = 2;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The third item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

第 5 步保存文件。

document.SaveToFile(@"result.docx", FileFormat.Docx);

结果

How to Create Multi-level List Numbering in Word in C#, VB.NET

完整代码

[C#]

using Spire.Doc;
using Spire.Doc.Documents;
namespace CreatList
{
class Program
{

static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();

ListStyle listStyle = new ListStyle(document, 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;
document.ListStyles.Add(listStyle);

Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ListLevelNumber = 1;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("A sub-sub-item");
paragraph.ApplyStyle(BuiltinStyle.Heading5);
paragraph.ListFormat.ListLevelNumber = 2;
paragraph.ListFormat.ApplyStyle("levelstyle");

paragraph = section.AddParagraph();
paragraph.AppendText("The third item");
paragraph.ApplyStyle(BuiltinStyle.Heading1);
paragraph.ListFormat.ApplyStyle("levelstyle");

document.SaveToFile(@"result.docx", FileFormat.Docx);

}
}
}

[VB.NET]

Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace CreatList
Class Program

Private Shared Sub Main(args As String())
Dim document As New Document()
Dim section As Section = document.AddSection()

Dim listStyle As New ListStyle(document, ListType.Numbered)
listStyle.Name = "levelstyle"
listStyle.Levels(0).PatternType = ListPatternType.Arabic
listStyle.Levels(1).NumberPrefix = vbNullChar & "."
listStyle.Levels(1).PatternType = ListPatternType.Arabic
listStyle.Levels(2).NumberPrefix = vbNullChar & "." & ChrW(1) & "."
listStyle.Levels(2).PatternType = ListPatternType.Arabic
document.ListStyles.Add(listStyle)

Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("The first item")
paragraph.ApplyStyle(BuiltinStyle.Heading1)
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The second item")
paragraph.ApplyStyle(BuiltinStyle.Heading1)
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The first sub-item")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
paragraph.ListFormat.ListLevelNumber = 1
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The second sub-item")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
paragraph.ListFormat.ContinueListNumbering()
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("A sub-sub-item")
paragraph.ApplyStyle(BuiltinStyle.Heading5)
paragraph.ListFormat.ListLevelNumber = 2
paragraph.ListFormat.ApplyStyle("levelstyle")

paragraph = section.AddParagraph()
paragraph.AppendText("The third item")
paragraph.ApplyStyle(BuiltinStyle.Heading1)
paragraph.ListFormat.ApplyStyle("levelstyle")

document.SaveToFile("result.docx", FileFormat.Docx)

End Sub
End Class
End Namespace

欢迎下载|体验更多E-iceblue产品

如需获取更多产品相关信息请咨询慧都在线客服  


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关产品
Spire.Office for .NET

专业的.NET Office套件,涵盖office文档创建、编辑、转换、管理和OCR内容识别等操作

Spire.Doc for .NET

Spire.Doc for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。

Aspose.Words for .NET

无需Microsoft Word也可在任何平台上满足Word文档的一切操作需求。

Aspose.PDF for .NET

PDF文档创建组件,无需Adobe Acrobat,也可以在任何平台上操作PDF文档。

VMProtect

新一代软件保护系统,将保护后的代码放到虚拟机中运行,代码反编译软件反破解。

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP