Word格式處理控件Aspose.Words for .NET教程——?jiǎng)?chuàng)建和編輯列表
Aspose.Words For .NET是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
>>Aspose.Words for .NET已經(jīng)更新至v20.4,Aspose.Words for .Net更新至新版本v20.4,主要新增5大特點(diǎn),包括:支持閱讀PDF文檔;提供了更改亞洲段落間距和縮進(jìn)的功能;為PDF渲染添加了圖像插值選項(xiàng);添加了新的模式3D形狀渲染;圖表數(shù)據(jù)標(biāo)簽和系列的擴(kuò)展API。,點(diǎn)擊下載體驗(yàn)
Microsoft Word文檔中的列表是一組列表格式屬性。每個(gè)列表最多可以具有9個(gè)級(jí)別,并且分別為每個(gè)級(jí)別定義了格式設(shè)置屬性,例如數(shù)字樣式,起始值,縮進(jìn),制表符位置等。List對(duì)象始終屬于ListCollection集合。
本主題描述如何使用Aspose.Words以編程方式使用列表。可以在文檔中使用列表來(lái)格式化,排列和強(qiáng)調(diào)文本。列表是組織文檔中數(shù)據(jù)的一種好方法,它使讀者更容易理解要點(diǎn)。
通過(guò)應(yīng)用列表格式創(chuàng)建列表
Aspose.Words允許通過(guò)應(yīng)用列表格式輕松創(chuàng)建列表。DocumentBuilder提供了DocumentBuilder.ListFormat屬性,該屬性返回ListFormat對(duì)象。該對(duì)象有幾種方法可以開(kāi)始和結(jié)束列表以及增加/減少縮進(jìn)量。 Microsoft Word中有兩種常規(guī)類(lèi)型的列表:項(xiàng)目符號(hào)和編號(hào)。
- 要啟動(dòng)項(xiàng)目符號(hào)列表,請(qǐng)調(diào)用 ListFormat.ApplyBulletDefault 。
- 要開(kāi)始編號(hào)列表,請(qǐng)調(diào)用 ListFormat.ApplyNumberDefault 。
項(xiàng)目符號(hào)或編號(hào)和格式將添加到當(dāng)前段落,并使用DocumentBuilder創(chuàng)建所有其他段落, 直到 調(diào)用ListFormat.RemoveNumbers停止項(xiàng)目符號(hào)列表格式。 在Word文檔中,列表最多可以包含九個(gè)級(jí)別。每個(gè)級(jí)別的列表格式指定使用的項(xiàng)目符號(hào)或編號(hào),左縮進(jìn),項(xiàng)目符號(hào)與文本之間的間距等。
- 要將當(dāng)前段落的列表級(jí)別增加一級(jí),請(qǐng)調(diào)用ListFormat.ListIndent。
- 要將當(dāng)前段落的列表級(jí)別降低一級(jí),請(qǐng)調(diào)用 ListFormat.ListOutdent。
下面的示例顯示了如何構(gòu)建多級(jí)列表。
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);
重新啟動(dòng)每個(gè)節(jié)的列表
使用List.IsRestartAtEachSection屬性可以重新啟動(dòng)每個(gè)節(jié)的列表。請(qǐng)注意,僅RTF,DOC和DOCX文檔格式支持此選項(xiàng)。僅當(dāng)OoxmlCompliance高于Ecma376時(shí),此選項(xiàng)才會(huì)寫(xiě)入DOCX。下面的代碼示例演示如何創(chuàng)建列表并為每個(gè)部分重新啟動(dòng)它。
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);
還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢(xún)和咨詢(xún)。