This is a migrated thread and some comments may be shown as answers.

Add TOC from code

5 Answers 94 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Erik
Top achievements
Rank 1
Erik asked on 11 May 2013, 07:51 AM
I would like to add a Table of Contents from code, is there documentation on how to do this?

5 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 14 May 2013, 01:20 PM
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.

0
Erik
Top achievements
Rank 1
answered on 16 May 2013, 05:43 PM
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?
0
Petya
Telerik team
answered on 18 May 2013, 03:46 PM
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.

0
Erik
Top achievements
Rank 1
answered on 19 May 2013, 10:57 AM
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
                },
            };
        }
    }
}


0
Petya
Telerik team
answered on 22 May 2013, 04:06 PM
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.

Tags
RichTextBox
Asked by
Erik
Top achievements
Rank 1
Answers by
Petya
Telerik team
Erik
Top achievements
Rank 1
Share this question
or