New to Telerik UI for WPF? Download free 30-day trial

Headers and Footers

RadRichTextBox supports Headers and Footers in its document when in Paged layout mode.

Note that Headers and Footers are not persisted when exporting with HtmlFormatProvider. You can read more about this here.

The topic contains the following sections:

The Headers and Footers are properties of a Section and each section in the document can have the following types of Headers and Footers:

  • First—Used only on the first page of the section.

  • Even—Used on all even numbered pages in the section.

  • Default—Used on all pages of the section, where First or Even are not applicable or not specified.

Customizing Headers and Footers

The following example demonstrates how you can create a Header.

Create Header

Header header = new Header() { Body = radDocument, IsLinkedToPrevious = false }; //radDocument represents the content of a Header,  
                                                                                 //typically contains a few paragraphs 

When it comes to using a Header created in this manner, this depends on the state of the document - if it has been measured or not.

  • In a non-measured document, which you are building in code-behind, you can set the Default page Header of a section as illustrated below:

    Set Header of a Section

        section.Headers.Default = header; 
    
  • In a measured document (a document that has been previewed in the editor), you can change the Default page header of the first section like shown in the next example:

    Update a Header in Measured Document

        this.radRichTextBox.UpdateHeader(this.radRichTextBox.Document.Sections.First, HeaderFooterType.Default, header); 
    

All header/footer types - Default, First and Even are set identically. The only thing you should add when you set the First or Even Header/Footer of the document, is to set the property of the section that notifies the document to use different Header/Footer than the default one using one of the properties illustrated below:

Set Different Even/Odd Header

this.radRichTextBox.Document.Sections.First.HasDifferentFirstPageHeaderFooter = true; 
//or 
this.radRichTextBox.Document.HasDifferentEvenAndOddHeadersFooters = true; 

Setting the Footers can be done in the same way. Here is the respective code for footers:

  • Creating a Footer:

    Create Footer

        Footer footer = new Footer() { Body = radDocument, IsLinkedToPrevious = false }; //radDocument is an instance of RadDocument, representing the content of the footer. 
    
  • Setting the Footer to be used in a particular section:

    • In a non-measured document:

      Set Footer of a Section

              section.Footers.Default = footer; 
      
    • In a measured document:

      Update a Footer in Measured Document

              this.radRichTextBox.UpdateFooter(this.radRichTextBox.Document.Sections.First, HeaderFooterType.Default, footer); 
      

As for setting different footers for the first page or the even page, this is done by passing the respective parameter to the UpdateFooter() method - HeaderFooterType.First or HeaderFooterType.Even and setting the corresponding property of the document/editor.

Set Different Even/Odd Footer

this.radRichTextBox.Document.Sections.First.HasDifferentFirstPageHeaderFooter = true; 
//or 
this.radRichTextBox.Document.HasDifferentEvenAndOddHeadersFooters = true; 

Disabling Headers and Footers

Headers and footers are only present in Paged layout mode, so the easiest way to remove them is to change the layout mode. In case you wish to show documents in paged mode and still disable headers and footers, you can do so by removing the UI layer responsible for their visualization - HeaderFooterLayer.

The concept of UI layers and their usage are explained in this article.

Disable Headers and Footers

class CustomUILayerBuilder : UILayersBuilder 
{ 
    protected override void BuildUILayersOverride(IUILayerContainer uiLayerContainer) 
    { 
        base.BuildUILayersOverride(uiLayerContainer); 
        uiLayerContainer.UILayers.Remove(DefaultUILayers.HeaderFooterLayer); 
    } 
} 

Accessing the Headers and Footers

The Headers and Footers can be retrieved, for example, via interating the Sections collection of the Document property of the RadRichTextBox control. Each Section instance exposes this information via its API.

Retrieving the Headers

foreach (Telerik.Windows.Documents.Model.Section section in this.radRichTextBox.Document.Sections) 
{ 
    if (!section.Headers.Default.IsEmpty) 
    { 
        RadDocument radDocument = new RadDocument(); 
        Section section1 = new Section(); 
        Paragraph paragraph = new Paragraph(); 
        Span span = new Span("Default"); 
        paragraph.Inlines.Add(span); 
        section.Blocks.Add(paragraph); 
 
        RadDocument document = new RadDocument(); 
        document.Sections.Add(section1); 
 
        section.Headers.Default.Body = document; 
    } 
 
    if (!section.Headers.First.IsEmpty) 
    { 
        RadDocument radDocument = new RadDocument(); 
        Section section1 = new Section(); 
        Paragraph paragraph = new Paragraph(); 
        Span span = new Span("First"); 
        paragraph.Inlines.Add(span); 
        section.Blocks.Add(paragraph); 
 
        RadDocument document = new RadDocument(); 
        document.Sections.Add(section1); 
 
        section.Headers.Default.Body = document; 
    } 
 
    if (!section.Headers.Even.IsEmpty) 
    { 
        RadDocument radDocument = new RadDocument(); 
        Section section1 = new Section(); 
        Paragraph paragraph = new Paragraph(); 
        Span span = new Span("Even"); 
        paragraph.Inlines.Add(span); 
        section.Blocks.Add(paragraph); 
 
        RadDocument document = new RadDocument(); 
        document.Sections.Add(section1); 
 
        section.Headers.Default.Body = document; 
    } 
} 

Additionally, the RadRichTextBox control exposes the ActiveDocumentEditor property. It provides information about the currently active editor that you can utilize to access the Headers and Footers.

More information about the ActiveDocumentEditor property can be found here.

See Also

In this article