使用列表

Microsoft Word 文档中的列表是一组段落格式属性。列表可在文档中用于构建、排列和突出显示文本。列表是组织文档中数据的好方法,可以让读者轻松吸收和理解要点。

每个列表最多可以有 9 个级别,并且为每个级别单独定义格式属性,例如数字样式、起始值、缩进、制表符位置等。

在 Aspose.Words 中,使用列表由 Lists 命名空间表示。但是,List 对象始终属于 ListCollection

本文介绍使用 Aspose.Words 以编程方式处理列表。

通过应用列表格式创建列表

Aspose.Words 允许通过应用列表格式轻松创建列表。 DocumentBuilder 提供返回 ListFormat 对象的 ListFormat 属性。该对象有多种方法来开始和结束列表以及增加/减少缩进。 Microsoft Word 中有两种常见类型的列表:项目符号列表和编号列表:

项目符号或编号和格式将添加到当前段落以及使用 DocumentBuilder 创建的所有其他段落,直到调用 RemoveNumbers 来停止项目符号列表格式设置。

在 Word 文档中,列表最多可以包含九个级别。每个级别的列表格式指定使用什么项目符号或编号、左缩进、项目符号和文本之间的空格等。以下方法更改列表级别并应用新级别的格式属性:

  • 要将当前段落的列表级别增加一级,请调用 ListIndent
  • 要将当前段落的列表级别降低一级,请调用 ListOutdent

您还可以使用 ListLevelNumber 属性来获取或设置段落的列表级别。

以下代码示例展示了如何构建多级列表:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ListFormat.ApplyNumberDefault();
builder.Writeln("Item 1");
builder.Writeln("Item 2");
builder.ListFormat.ListIndent();
builder.Writeln("Item 2.1");
builder.Writeln("Item 2.2");
builder.ListFormat.ListIndent();
builder.Writeln("Item 2.2.1");
builder.Writeln("Item 2.2.2");
builder.ListFormat.ListOutdent();
builder.Writeln("Item 2.3");
builder.ListFormat.ListOutdent();
builder.Writeln("Item 3");
builder.ListFormat.RemoveNumbers();
dataDir = dataDir + "DocumentBuilderSetMultilevelListFormatting_out.doc";
doc.Save(dataDir);

指定列表级别的格式

创建列表时会自动创建列表级对象。使用 ListLevel 类的属性和方法来控制列表各个级别的格式。

每个部分的重新启动列表

您可以使用 IsRestartAtEachSection 属性重新启动每个部分的列表。请注意,仅 RTF、DOC 和 DOCX 文档格式支持此选项。仅当 OoxmlCompliance 高于 Ecma376 时,此选项才会写入 DOCX。

以下代码示例展示了如何创建列表并为每个部分重新启动它:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
doc.Lists.Add(ListTemplate.NumberDefault);
List list = doc.Lists[0];
// Set true to specify that the list has to be restarted at each section.
list.IsRestartAtEachSection = true;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ListFormat.List = list;
for (int i = 1; i < 45; i++)
{
builder.Writeln(String.Format("List Item {0}", i));
// Insert section break.
if (i == 15)
builder.InsertBreak(BreakType.SectionBreakNewPage);
}
// IsRestartAtEachSection will be written only if compliance is higher then OoxmlComplianceCore.Ecma376
OoxmlSaveOptions options = new OoxmlSaveOptions();
options.Compliance = OoxmlCompliance.Iso29500_2008_Transitional;
dataDir = dataDir + "RestartAtEachSection_out.docx";
// Save the document to disk.
doc.Save(dataDir, options);