|
| 1 | +--- |
| 2 | +title: Styling Table of Contents with Multilevel List Numbering in Telerik WordsProcessing |
| 3 | +description: Learn how to create and style a table of contents (TOC) with hierarchical, multilevel list numbering in Telerik WordsProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: Applying Multilevel List Numbering to Table of Contents in Telerik WordsProcessing |
| 6 | +meta_title: Applying Multilevel List Numbering to Table of Contents in Telerik WordsProcessing |
| 7 | +slug: wordsprocessing-styling-table-of-contents-multilevel-list-numbering |
| 8 | +tags: words, processing, telerik, document, processing, table, contents, toc, hierarchical, numbering, styling, multilevel, list, word, docx |
| 9 | +res_type: kb |
| 10 | +ticketid: 1698635 |
| 11 | +--- |
| 12 | + |
| 13 | +# Environment |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2025.3.806 | RadWordsProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +This article describes how to apply multilevel list numbering and other specific styles to each level of [Table Of Contents]({%slug radwordsprocessing-concepts-toc-field%}) (TOC) in a DOCX document, such as font size, indentations, and font weight. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to add multilevel list numbering to a TOC in Telerik WordsProcessing? |
| 24 | +- How to customize TOC level styles programmatically? |
| 25 | +- How to use ParagraphProperties and CharacterProperties to style a TOC? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To achieve a multilevel list numbering in your [Table Of Contents]({%slug radwordsprocessing-concepts-toc-field%}) (TOC) and apply custom styles to each level, follow these steps: |
| 30 | + |
| 31 | +1. Set the [`NumberingFieldsProvider`]({%slug radpdfprocessing-formats-and-conversion-pdf-numbering-fields-provider%}) for the hierarchical numbering functionality. |
| 32 | +2. Load your document using the [`DocxFormatProvider`]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%}). |
| 33 | +3. Add a table of contents field using the [`RadFlowDocumentEditor`]({%slug radwordsprocessing-editing-radflowdocumenteditor%}). |
| 34 | +4. Configure hierarchical numbering by adding a list of type [`NumberedHierarchical`]({%slug radwordsprocessing-concepts-lists%}). |
| 35 | +5. Style each TOC level by specifying its properties using [`ParagraphProperties`]({%slug radwordsprocessing-concepts-style-properties%}#style-properties-in-styles) and [`CharacterProperties`]({%slug radwordsprocessing-concepts-style-properties%}#style-properties-in-styles). |
| 36 | + |
| 37 | +### Implementation |
| 38 | + |
| 39 | +```csharp |
| 40 | +using System; |
| 41 | +using System.IO; |
| 42 | +using System.Diagnostics; |
| 43 | +using Telerik.Windows.Documents.Flow.Model; |
| 44 | +using Telerik.Windows.Documents.Flow.FormatProviders.Docx; |
| 45 | +using Telerik.Windows.Documents.Flow.Extensibility; |
| 46 | + |
| 47 | +FlowExtensibilityManager.NumberingFieldsProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.NumberingFieldsProvider(); |
| 48 | + |
| 49 | +RadFlowDocument flowDocument; |
| 50 | +DocxFormatProvider docxFormatProvider = new DocxFormatProvider(); |
| 51 | + |
| 52 | +// Load the document. |
| 53 | +using (Stream input = File.OpenRead("input.docx")) |
| 54 | +{ |
| 55 | + flowDocument = docxFormatProvider.Import(input, null); |
| 56 | +} |
| 57 | + |
| 58 | +var editor = new RadFlowDocumentEditor(flowDocument); |
| 59 | + |
| 60 | +// Insert table of contents field. |
| 61 | +FieldInfo tocFieldInfo = editor.InsertField("TOC \f a"); |
| 62 | +tocFieldInfo.UpdateField(); |
| 63 | + |
| 64 | +// Create hierarchical numbering list for the TOC. |
| 65 | +var tocList = flowDocument.Lists.Add(ListTemplateType.NumberedHierarchical); |
| 66 | +tocList.MultilevelType = MultilevelType.Multilevel; |
| 67 | + |
| 68 | +// Style TOC Level 1 |
| 69 | +var tocLevel1StyleId = BuiltInStyleNames.GetTocStyleIdByIndex(1); |
| 70 | +flowDocument.StyleRepository.AddBuiltInStyle(tocLevel1StyleId); |
| 71 | +var tocLevel1Style = flowDocument.StyleRepository.GetStyle(tocLevel1StyleId); |
| 72 | +tocLevel1Style.CharacterProperties.FontSize.LocalValue = 13; |
| 73 | +tocLevel1Style.ParagraphProperties.LeftIndent.LocalValue = 0; |
| 74 | +tocLevel1Style.CharacterProperties.FontWeight.LocalValue = FontWeights.Bold; |
| 75 | +tocLevel1Style.ParagraphProperties.HangingIndent.LocalValue = 35; |
| 76 | +tocLevel1Style.ParagraphProperties.ListLevel.LocalValue = 0; |
| 77 | +tocLevel1Style.ParagraphProperties.ListId.LocalValue = tocList.Id; |
| 78 | + |
| 79 | +// Style TOC Level 2 |
| 80 | +var tocLevel2StyleId = BuiltInStyleNames.GetTocStyleIdByIndex(2); |
| 81 | +flowDocument.StyleRepository.AddBuiltInStyle(tocLevel2StyleId); |
| 82 | +var tocLevel2Style = flowDocument.StyleRepository.GetStyle(tocLevel2StyleId); |
| 83 | +tocLevel2Style.ParagraphProperties.ListLevel.LocalValue = 1; |
| 84 | +tocLevel2Style.ParagraphProperties.LeftIndent.LocalValue = 35; |
| 85 | +tocLevel2Style.ParagraphProperties.HangingIndent.LocalValue = 35; |
| 86 | +tocLevel2Style.CharacterProperties.FontSize.LocalValue = 13; |
| 87 | +tocLevel2Style.ParagraphProperties.ListId.LocalValue = tocList.Id; |
| 88 | + |
| 89 | +// Style TOC Level 3 |
| 90 | +var tocLevel3StyleId = BuiltInStyleNames.GetTocStyleIdByIndex(3); |
| 91 | +flowDocument.StyleRepository.AddBuiltInStyle(tocLevel3StyleId); |
| 92 | +var tocLevel3Style = flowDocument.StyleRepository.GetStyle(tocLevel3StyleId); |
| 93 | +tocLevel3Style.ParagraphProperties.ListLevel.LocalValue = 2; |
| 94 | +tocLevel3Style.ParagraphProperties.LeftIndent.LocalValue = 65; |
| 95 | +tocLevel3Style.ParagraphProperties.HangingIndent.LocalValue = 35; |
| 96 | +tocLevel3Style.CharacterProperties.FontSize.LocalValue = 13; |
| 97 | +tocLevel3Style.ParagraphProperties.ListId.LocalValue = tocList.Id; |
| 98 | + |
| 99 | +// Export the styled document |
| 100 | +string docxOutputPath = "..\\..\\..\\output.docx"; |
| 101 | +File.Delete(docxOutputPath); |
| 102 | +using (Stream output = File.OpenWrite(docxOutputPath)) |
| 103 | +{ |
| 104 | + docxFormatProvider.Export(flowDocument, output, null); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## See Also |
| 109 | + |
| 110 | +* [WordsProcessing Overview]({%slug radwordsprocessing-overview%}) |
| 111 | +* [Table of Contents Field]({%slug radwordsprocessing-concepts-toc-field%}) |
| 112 | +* [ParagraphProperties API Reference]({%slug radwordsprocessing-concepts-style-properties%}#style-properties-in-styles) |
| 113 | +* [CharacterProperties API Reference]({%slug radwordsprocessing-concepts-style-properties%}#style-properties-in-styles) |
0 commit comments