Word格式处理控件Aspose.Words for .NET教程——创建和编辑列表

Aspose.Words For .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

>>Aspose.Words for .NET已经更新至最新版,主要新增5大特点,包括:支持阅读PDF文档;提供了更改亚洲段落间距和缩进的功能;为PDF渲染添加了图像插值选项;添加了新的模式3D形状渲染;图表数据标签和系列的扩展API。,点击下载体验

Aspose文档管理产品  Aspose技术交流群(761297826)


Microsoft Word文档中的列表是一组列表格式属性。每个列表最多可以具有9个级别,并且分别为每个级别定义了格式设置属性,例如数字样式,起始值,缩进,制表符位置等。List对象始终属于ListCollection集合。

本主题描述如何使用Aspose.Words以编程方式使用列表。可以在文档中使用列表来格式化,排列和强调文本。列表是组织文档中数据的一种好方法,它使读者更容易理解要点。

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

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

  • 要启动项目符号列表,请调用 ListFormat.ApplyBulletDefault 。
  • 要开始编号列表,请调用 ListFormat.ApplyNumberDefault 。

项目符号或编号和格式将添加到当前段落,并使用DocumentBuilder创建所有其他段落, 直到 调用ListFormat.RemoveNumbers停止项目符号列表格式。 在Word文档中,列表最多可以包含九个级别。每个级别的列表格式指定使用的项目符号或编号,左缩进,项目符号与文本之间的间距等。

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

下面的示例显示了如何构建多级列表。

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

重新启动每个节的列表

使用List.IsRestartAtEachSection属性可以重新启动每个节的列表。请注意,仅RTF,DOC和DOCX文档格式支持此选项。仅当OoxmlCompliance高于Ecma376时,此选项才会写入DOCX。下面的代码示例演示如何创建列表并为每个部分重新启动它。

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值