Creating Table of Content

Brief

The article describes how to create a table of content in a document.

Details

You can create a table of content (TOC), or an outline, at the end or in some other place in your document. A TOC includes the paragraphs marked as TOC items which precede the TOC, no matter whether they are in one section or not. You can have several TOCs in one document. Items in a TOC are clickable, so you can quickly navigate to the corresponding paragraphs in the document.

To mark a paragraph as a TOC item, call the method ParagraphBuilder.SetOutline. In this case, the paragraph text and page number will appear in the TOC.
If you call the method without the parameter, the item level will be set to 0 and have no indent. If you create a multilevel TOC, specify the item level in the OutlineLevel parameter. The items with non-zero level values will be shifted to the left depending on their level.

To add a TOC to a document, call one of the AddOutline() methods of the OutlineExtensions class for your document. When you add a TOC to your document, a separate section is created for it under the hood, so you can configure the margin, paper size, and page orientation settings for your TOC. If you do not specify any of these settings, they will have their default values defined by the default style, that is, 20f margins, the Letter paper size, and the Landscape page orientation. Along with these settings, you can configure the settings specific for the TOC: its font, caption, indent, and caption font. If not specified, they will also have their default values defined by the default style.

Both methods return an instance of the OutlineBuilder class that is derived from the FormattedTextElementBuilder, so you can use all its methods to configure the TOC, for example, SetFont(), SetUnderline().

You can configure the TOC using methods of the OutlineBuilder class.

To set the text and font for the TOC caption, you can use one of the following options:

To set the background color for the caption, call the SetCaptionBackColor method.

You can also configure the line leader between the texts of TOC items and the page numbers using the methods SetSpacingUnderline, SetSpacingUnderlineStroke, and SetSpacingUnderlineColor.



See also

Creating Document

Adding Paragraph



Examples

Example 1. Create a simple table of content Hide

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Chapter I")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Chapter II")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter II")
                .ToSection()
                    .AddParagraph("Chapter III")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter III")
            .ToDocument()
                .AddOutline()
            .ToDocument()
                .Build("Result.pdf");
 
 The above code will generate the following:
 
 
 See the document

Example 2. Create a multilevel table of content Hide

            DocumentBuilder.New()
             // Add content and mark some paragraphs as TOC items:
                .AddSection()
                    .AddParagraph("Chapter I")
                     // Set the higher level:
                        .SetOutline(0)
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Chapter I.1")
                     // Set the first sub-level:
                        .SetOutline(1)
                .ToSection()
                    .AddParagraph("Text of chapter I.1")
                .ToSection()
                    .AddParagraph("Chapter I.2")
                        .SetOutline(1)
                .ToSection()
                    .AddParagraph("Text of chapter I.2")
                .ToSection()
                    .AddParagraph("Chapter I.2.a")
                     // Set the second sub-level:
                        .SetOutline(2)
                .ToSection()
                    .AddParagraph("Text of chapter I.2.a")
                .ToSection()
                    .AddParagraph("Chapter I.2.a")
                        .SetOutline(2)
                .ToSection()
                    .AddParagraph("Text of chapter I.2.a")
                .ToSection()
                    .AddParagraph("Chapter II")
                        .SetOutline(0)
            .ToDocument()
             // Add a TOC to the document and set the left indent to 20:
                .AddOutline(outlineCaption:"Outline", levelLeftIndent:20)
            .ToDocument()
                .Build("Result.pdf");
 
 The above code will generate the following:
 
 
 See the document

Example 3. Create a table of content in the middle of a document Hide

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Chapter I")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Chapter II")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter II")
            .ToDocument()
                .AddOutline()
            .ToDocument()
                .AddSection()
                    .AddParagraph("Chapter III")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter III")
            .ToDocument()
                .Build("Result.pdf");
 
 The above code will generate the following:
 
 
 See the document

Example 4. Create a table of content with a line leader Hide

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Chapter I")
                        .SetOutline(0)
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Chapter I.1")
                        .SetOutline(1)
                .ToSection()
                    .AddParagraph("Text of chapter I.1")
                .ToSection()
                    .AddParagraph("Chapter I.1.a")
                        .SetOutline(2)
                .ToSection()
                    .AddParagraph("Text of chapter I.1.a")
            .ToDocument()
                .AddOutline()
                 // Set the Red Dotted line leader between the TOC item text and the page number:
                    .SetSpacingUnderline(Stroke.Dotted, Color.Red)
                 // Shift levels from the left:
                    .SetLevelLeftIndent(15)
            .ToDocument()
                .Build("Result.pdf");
 
 The above code will generate the following:
 
 
 See the document

Example 5. Create a table of content with setting fonts and colors using the parameters of the [c]AddOutline[/c] method Hide

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Chapter I")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Chapter II")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter II")
            .ToDocument()
             // Set Fonts in the AddOutline method:
                .AddOutline(
                    outlineCaption: "Outline",
                    outlineFont: Fonts.Courier(16).SetColor(Color.Green),
                    captionFont: Fonts.Times(20).SetColor(Color.Red).SetBold().SetUnderline(Stroke.Double, Color.Blue)
                        )
                 // Spacing color will be Green as defined by the TOC font:
                    .SetSpacingUnderlineStroke(Stroke.Dashed)
            .ToDocument()
                .Build("Result.pdf");
 
 The above code will generate the following:
 
 
 See the document

Example 6. Create a table of content with setting fonts and colors using the methods of the [c]OutlineBuilder[/c] class Hide

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Chapter I")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Chapter II")
                            !.SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter II")
            .ToDocument()
                .AddOutline("Outline")
                 // Set Fonts using the OutlineBuilder methods:
                    .SetCaptionFont(Fonts.Times(20).SetColor(Color.Red).SetBold().SetUnderline(Stroke.Double, Color.Blue))
                    .SetOutlineFont(Fonts.Courier(16).SetColor(Color.Green))
                 // The line leader color will be Green as defined by the TOC font:
                    .SetSpacingUnderlineStroke(Stroke.Dashed)
            .ToDocument()
                .Build("Result.pdf");
 
 The above code will generate the following:
 
 
 See the document

Example 7. Support of several sections Hide

            DocumentBuilder.New()
             // Section 1:
                .AddSection()
                    .AddParagraph("Section 1. Chapter I")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Section 1. Chapter II")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter II")
            .ToDocument()
             // Section 2:
                .AddSection()
                    .AddParagraph("Section 2. Chapter I")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter I")
                .ToSection()
                    .AddParagraph("Section 2. Chapter II")
                        .SetOutline()
                .ToSection()
                    .AddParagraph("Text of chapter II")
            .ToDocument()
             // This TOC will contain items from both sections before calling AddOutline:
                .AddOutline("Outline")
            .ToDocument()
                .Build("Result.pdf");
 
 The above code will generate the following:
 
 
 See the document

back