Add TOC from code

6 posts, 0 answers
  1. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 11 May 2013 Link to this post

    I would like to add a Table of Contents from code, is there documentation on how to do this?
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 14 May 2013 Link to this post

    Hi Erik,

    You can insert a TOC filed at the caret position from code like this:
    this.radRichTextBox.InsertField(new TableOfContentsField(), FieldDisplayMode.Result);

    I hope this helps!
     
    Regards,
    Petya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 16 May 2013 Link to this post

    Hi Petya, thanks for the reply.

    I've tried to do it as follows:
    var document = new RadDocument();
    document.InsertField(new TableOfContentsField(), FieldDisplayMode.Result);

    the result shows the text: "No table of contents entries found." When I export the document to a Word document, in Word I can then right click and select "Update Field", after which the TOC shows all the heading entries. Is it possible to force the RadDocument to update the TOC to show the content?
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 18 May 2013 Link to this post

    Hello Erik,

    As you can see in my post below, you should insert a TOC filed using the RadRichTextBox instance in which a document is shown. In case you want to insert the field in a document that will not be shown in the UI, please follow the suggestions from this help article and use a RadDocumentEditor.

    Regarding the No table of contents entries found message, if you've inserted the TOC filed prior all other content, you'd have to update the field. This can be achieved from code as follows:
    var tocField = this.radRichTextBox.Document.EnumerateChildrenOfType<FieldRangeStart>().Where(x=>x.Field is TableOfContentsField).FirstOrDefault();
    if(tocField != null)
    {
        this.radRichTextBox.UpdateField(tocField);
    }

    Let us know if you have other questions.
     
    Kind regards,
    Petya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 19 May 2013 Link to this post

    Hi Petya,

    It seems the issue lies with the way I define the headings style.
    In the code below I add one header using a default heading style, this header is shown in the TOC (also see attached screenshot)
    I also create my own style for a second heading, this heading does not show up in the TOC. If I export to Word and manually update the TOC, it does shown up. Am I creating the header style wrong?

    using System.Windows.Media;
    using Telerik.Windows.Documents.Model;
    using Telerik.Windows.Documents.Model.Styles;
     
    namespace RadRichtTextBoxTOCTest
    {
        public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
     
                var document = new RadDocument();
                var section = new Section();
     
                var paragraph1 = new Paragraph { StyleName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(1) };
                paragraph1.Inlines.Add(new Span("Heading 1 is shown in TOC"));
                section.Blocks.Add(paragraph1);
     
                var paragraph2 = new Paragraph { Style = GetHeadingStyle() };
                paragraph2.Inlines.Add(new Span("Heading 2 is not shown in TOC"));
                section.Blocks.Add(paragraph2);
     
                document.Sections.Add(section);
     
                RichTextBox.Document = document;
     
                var toc = new TableOfContentsField();
                RichTextBox.InsertField(toc, FieldDisplayMode.Result);
                toc.Update();
            }
     
            private static StyleDefinition GetHeadingStyle()
            {
                return new StyleDefinition
                {
                    DisplayName = "Test Heading 1",
                    Name = "Test Heading 1",
                    Type = StyleType.Paragraph,
                    BasedOnName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(1),
                    ParagraphProperties =
                    {
                        LeftIndent = 6,
                        SpacingBefore = 20,
                        SpacingAfter = 0,
                    },
                    SpanProperties =
                    {
                        //FontFamily = new FontFamily("Cambria"),
                        ForeColor = Colors.Green,
                        //FontWeight = fontWeight,
                        //FontSize = fontSize,
                        //FontStyle = fontStyle
                    },
                };
            }
        }
    }


  7. Petya
    Admin
    Petya avatar
    975 posts

    Posted 22 May 2013 Link to this post

    Hello Eric,

    You can specify whether or not a paragraph should be added to the TOC using its OutlineLevel property. You can set it to the style definition you are creating through the ParagraphProperties:
    return new StyleDefinition
         {
             DisplayName = "Test Heading 1",
             Name = "Test Heading 1",
             Type = StyleType.Paragraph,
             BasedOnName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(1),
             ParagraphProperties =
             {
                 OutlineLevel=1,
             },//...
    Additionally, you have to specify that the TOC you are inserting should show outline levels:
    var toc = new TableOfContentsField() { UseParagraphsOutlineLevels = true };

    On the other hand, Headings are linked styles, so in order to truly base a style on them, you'd have to create a linked style as well. With the following code you will also inherit the OutlineLevel of the Heading, so there is no need to explicitly set the property:
    StyleDefinition style = new StyleDefinition()
               {
                   DisplayName = "TestHeading1",
                   Name = "TestHeading1",
                   Type = StyleType.Paragraph,
                   NextStyleName = RadDocumentDefaultStyles.NormalStyleName,
                   BasedOnName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(1),
                   LinkedStyle = new StyleDefinition()
                   {
                       Name = "CharTestHeading1",
                       DisplayName = "CharTestHeading1",
                       Type = StyleType.Character,
                       BasedOnName = RadDocumentDefaultStyles.GetHeadingCharDisplayStyleNameByIndex(1)
                   }
               };

    I hope this helps!
     
    Regards,
    Petya
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
UI for WPF is Visual Studio 2017 Ready