Header and Footer Example for WPF

Nevron NOV RichTextEditor for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF RichTextEditor examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Header and Footer Example for WPF Description

This example demonstrates how create sections with different header and footer. Each section has three different properties for header and footer that allow you to specify distinct header and footer for first, left and right pages.


Header and Footer Example

C# Header and Footer Example Code for Nevron RichTextEditor for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Text;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NHeaderAndFooterExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			// Create the rich text
			NRichTextViewWithRibbon richTextWithRibbon = new NRichTextViewWithRibbon();
			m_RichText = richTextWithRibbon.View;
			m_RichText.AcceptsTab = true;
			m_RichText.Content.Sections.Clear();

			// Populate the rich text
			PopulateRichText();

			return richTextWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			return null;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how create sections with different header and footer. Each section has three different properties for header and footer that allow you to specify distinct header and footer for first, left and right pages.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Rich Text Sections", "https://www.nevron.com/docs/openvision/rich-text-sections.html"), ("Rich Text Block Layout", "https://www.nevron.com/docs/openvision/rich-text-block-layout.html"), ("Rich Text Field Inlines", "https://www.nevron.com/docs/openvision/rich-text-field-inlines.html"), }; } private void PopulateRichText() { m_RichText.Content.Layout = ENTextLayout.Print; for (int sectionIndex = 0; sectionIndex < 3; sectionIndex++) { NSection section = new NSection(); NColor color = NColor.Empty; string sectionText = string.Empty; switch (sectionIndex) { case 0: { sectionText = "Red Section (uniform header and footer)."; color = NColor.Red; section.Blocks.Add(GetDescriptionBlock("Section Headers and Footers", "This example shows how to create sections with different header and footer settings.", 1)); section.DifferentFirstHeaderAndFooter = false; section.DifferentLeftRightHeadersAndFooters = false; NHeaderFooter header = CreateHeaderFooter("Uniform Header."); header.BackgroundFill = new NColorFill(color); section.Header = header; NHeaderFooter footer = CreateHeaderFooter("Uniform Footer."); footer.BackgroundFill = new NColorFill(color); section.Footer = footer; } break; case 1: { sectionText = "Green Section (different first header and footer)."; color = NColor.Green; section.BreakType = ENSectionBreakType.NextPage; section.DifferentFirstHeaderAndFooter = true; section.DifferentLeftRightHeadersAndFooters = false; NHeaderFooter headerFirst = CreateHeaderFooter("First Page Header."); headerFirst.BackgroundFill = new NColorFill(color); section.HeaderFirst = headerFirst; NHeaderFooter footerFirst = CreateHeaderFooter("First Page Footer."); footerFirst.BackgroundFill = new NColorFill(color); section.FooterFirst = footerFirst; } break; case 2: { sectionText = "Blue Section (different left and right page header and footer)."; color = NColor.Blue; section.BreakType = ENSectionBreakType.NextPage; section.DifferentFirstHeaderAndFooter = false; section.DifferentLeftRightHeadersAndFooters = true; NHeaderFooter headerLeft = CreateHeaderFooter("Left Page Header."); headerLeft.BackgroundFill = new NColorFill(color); section.HeaderLeft = headerLeft; NHeaderFooter headerRight = CreateHeaderFooter("Right Page Header."); headerRight.BackgroundFill = new NColorFill(color); section.HeaderRight = headerRight; NHeaderFooter footerLeft = CreateHeaderFooter("Left Page Footer."); footerLeft.BackgroundFill = new NColorFill(color); section.FooterLeft = footerLeft; NHeaderFooter footerRight = CreateHeaderFooter("Right Page Footer."); footerRight.BackgroundFill = new NColorFill(color); section.FooterRight = footerRight; } break; } m_RichText.Content.Sections.Add(section); // add some section contentparagraphs with some simple text for (int i = 0; i < 20; i++) { NParagraph paragraph = new NParagraph(); NTextInline textInline = new NTextInline(GetRepeatingText(sectionText, 5)); textInline.Fill = new NColorFill(color); paragraph.Inlines.Add(textInline); section.Blocks.Add(paragraph); } } } #endregion #region Implementation private NHeaderFooter CreateHeaderFooter(string text) { NHeaderFooter headerFooter = new NHeaderFooter(); NParagraph paragraph = new NParagraph(); paragraph.Inlines.Add(new NTextInline(text)); paragraph.Inlines.Add(new NTextInline(". Page ")); paragraph.Inlines.Add(new NFieldInline(ENNumericFieldName.PageNumber)); paragraph.Inlines.Add(new NTextInline(" of ")); paragraph.Inlines.Add(new NFieldInline(ENNumericFieldName.PageCount)); headerFooter.Blocks.Add(paragraph); return headerFooter; } #endregion #region Event Handlers #endregion #region Fields private NRichTextView m_RichText; #endregion #region Static Methods private static NParagraph GetDescriptionParagraph(string text) { return new NParagraph(text); } private static NParagraph GetTitleParagraphNoBorder(string text, int level) { double fontSize = 10; ENFontStyle fontStyle = ENFontStyle.Regular; switch (level) { case 1: fontSize = 16; fontStyle = ENFontStyle.Bold; break; case 2: fontSize = 10; fontStyle = ENFontStyle.Bold; break; } NParagraph paragraph = new NParagraph(); paragraph.HorizontalAlignment = ENAlign.Left; paragraph.FontSize = fontSize; paragraph.FontStyle = fontStyle; NTextInline textInline = new NTextInline(text); textInline.FontStyle = fontStyle; textInline.FontSize = fontSize; paragraph.Inlines.Add(textInline); return paragraph; } /// /// Gets a paragraph with title formatting /// /// /// private static NParagraph GetTitleParagraph(string text, int level) { NColor color = NColor.Black; NParagraph paragraph = GetTitleParagraphNoBorder(text, level); paragraph.HorizontalAlignment = ENAlign.Left; paragraph.Border = CreateLeftTagBorder(color); paragraph.BorderThickness = defaultBorderThickness; return paragraph; } private static NGroupBlock GetNoteBlock(string text, int level) { NColor color = NColor.Red; NParagraph paragraph = GetTitleParagraphNoBorder("Note", level); NGroupBlock groupBlock = new NGroupBlock(); groupBlock.ClearMode = ENClearMode.All; groupBlock.Blocks.Add(paragraph); groupBlock.Blocks.Add(GetDescriptionParagraph(text)); groupBlock.Border = CreateLeftTagBorder(color); groupBlock.BorderThickness = defaultBorderThickness; return groupBlock; } private static NGroupBlock GetDescriptionBlock(string title, string description, int level) { NColor color = NColor.Black; NParagraph paragraph = GetTitleParagraphNoBorder(title, level); NGroupBlock groupBlock = new NGroupBlock(); groupBlock.ClearMode = ENClearMode.All; groupBlock.Blocks.Add(paragraph); groupBlock.Blocks.Add(GetDescriptionParagraph(description)); groupBlock.Border = CreateLeftTagBorder(color); groupBlock.BorderThickness = defaultBorderThickness; return groupBlock; } /// /// Creates a left tag border with the specified border /// /// /// private static NBorder CreateLeftTagBorder(NColor color) { NBorder border = new NBorder(); border.LeftSide = new NBorderSide(); border.LeftSide.Fill = new NColorFill(color); return border; } private static NParagraph GetLoremIpsumParagraph() { return new NParagraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum placerat in tortor nec tincidunt. Sed sagittis in sem ac auctor. Donec scelerisque molestie eros, a dictum leo fringilla eu. Vivamus porta urna non ullamcorper commodo. Nulla posuere sodales pellentesque. Donec a erat et tortor viverra euismod non et erat. Donec dictum ante eu mauris porta, eget suscipit mi ultrices. Nunc convallis adipiscing ligula, non pharetra dolor egestas at. Etiam in condimentum sapien. Praesent sagittis pulvinar metus, a posuere mauris aliquam eget."); } /// /// Gets the specified text repeated /// /// /// /// private static string GetRepeatingText(string text, int count) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < count; i++) { if (builder.Length > 0) { builder.Append(" "); } builder.Append(text); } return builder.ToString(); } #endregion #region Constants private static readonly NMargins defaultBorderThickness = new NMargins(5.0, 0.0, 0.0, 0.0); #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Header and Footer Example for WPF Solution

VB.NET Header and Footer Example Code for Nevron RichTextEditor for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System.Text

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NHeaderAndFooterExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create the rich text
            Dim richTextWithRibbon As NRichTextViewWithRibbon = New NRichTextViewWithRibbon()
            m_RichText = richTextWithRibbon.View
            m_RichText.AcceptsTab = True
            m_RichText.Content.Sections.Clear()

            ' Populate the rich text
            PopulateRichText()

            Return richTextWithRibbon
        End Function
        Public Function CreateExampleControls() As NWidget
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how create sections with different header and footer. Each section has three different properties for header and footer that allow you to specify distinct header and footer for first, left and right pages.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Rich Text Sections", "https://www.nevron.com/docs/openvision/rich-text-sections.html"), ("Rich Text Block Layout", "https://www.nevron.com/docs/openvision/rich-text-block-layout.html"), ("Rich Text Field Inlines", "https://www.nevron.com/docs/openvision/rich-text-field-inlines.html")} End Function Private Sub PopulateRichText() m_RichText.Content.Layout = ENTextLayout.Print For sectionIndex = 0 To 2 Dim section As NSection = New NSection() Dim color = NColor.Empty Dim sectionText = String.Empty Select Case sectionIndex Case 0 sectionText = "Red Section (uniform header and footer)." color = NColor.Red section.Blocks.Add(GetDescriptionBlock("Section Headers and Footers", "This example shows how to create sections with different header and footer settings.", 1)) section.DifferentFirstHeaderAndFooter = False section.DifferentLeftRightHeadersAndFooters = False Dim header = CreateHeaderFooter("Uniform Header.") header.BackgroundFill = New NColorFill(color) section.Header = header Dim footer = CreateHeaderFooter("Uniform Footer.") footer.BackgroundFill = New NColorFill(color) section.Footer = footer Case 1 sectionText = "Green Section (different first header and footer)." color = NColor.Green section.BreakType = ENSectionBreakType.NextPage section.DifferentFirstHeaderAndFooter = True section.DifferentLeftRightHeadersAndFooters = False Dim headerFirst = CreateHeaderFooter("First Page Header.") headerFirst.BackgroundFill = New NColorFill(color) section.HeaderFirst = headerFirst Dim footerFirst = CreateHeaderFooter("First Page Footer.") footerFirst.BackgroundFill = New NColorFill(color) section.FooterFirst = footerFirst Case 2 sectionText = "Blue Section (different left and right page header and footer)." color = NColor.Blue section.BreakType = ENSectionBreakType.NextPage section.DifferentFirstHeaderAndFooter = False section.DifferentLeftRightHeadersAndFooters = True Dim headerLeft = CreateHeaderFooter("Left Page Header.") headerLeft.BackgroundFill = New NColorFill(color) section.HeaderLeft = headerLeft Dim headerRight = CreateHeaderFooter("Right Page Header.") headerRight.BackgroundFill = New NColorFill(color) section.HeaderRight = headerRight Dim footerLeft = CreateHeaderFooter("Left Page Footer.") footerLeft.BackgroundFill = New NColorFill(color) section.FooterLeft = footerLeft Dim footerRight = CreateHeaderFooter("Right Page Footer.") footerRight.BackgroundFill = New NColorFill(color) section.FooterRight = footerRight End Select m_RichText.Content.Sections.Add(section) ' add some section contentparagraphs with some simple text For i = 0 To 19 Dim paragraph As NParagraph = New NParagraph() Dim textInline As NTextInline = New NTextInline(GetRepeatingText(sectionText, 5)) textInline.Fill = New NColorFill(color) paragraph.Inlines.Add(textInline) section.Blocks.Add(paragraph) Next Next End Sub #End Region #Region "Implementation" Private Function CreateHeaderFooter(text As String) As NHeaderFooter Dim headerFooter As NHeaderFooter = New NHeaderFooter() Dim paragraph As NParagraph = New NParagraph() paragraph.Inlines.Add(New NTextInline(text)) paragraph.Inlines.Add(New NTextInline(". Page ")) paragraph.Inlines.Add(New NFieldInline(ENNumericFieldName.PageNumber)) paragraph.Inlines.Add(New NTextInline(" of ")) paragraph.Inlines.Add(New NFieldInline(ENNumericFieldName.PageCount)) headerFooter.Blocks.Add(paragraph) Return headerFooter End Function #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_RichText As NRichTextView #End Region #Region "Static Methods" Private Shared Function GetDescriptionParagraph(text As String) As NParagraph Return New NParagraph(text) End Function Private Shared Function GetTitleParagraphNoBorder(text As String, level As Integer) As NParagraph Dim fontSize As Double = 10 Dim fontStyle = ENFontStyle.Regular Select Case level Case 1 fontSize = 16 fontStyle = ENFontStyle.Bold Case 2 fontSize = 10 fontStyle = ENFontStyle.Bold End Select Dim paragraph As NParagraph = New NParagraph() paragraph.HorizontalAlignment = ENAlign.Left paragraph.FontSize = fontSize paragraph.FontStyle = fontStyle Dim textInline As NTextInline = New NTextInline(text) textInline.FontStyle = fontStyle textInline.FontSize = fontSize paragraph.Inlines.Add(textInline) Return paragraph End Function ''' ''' Gets a paragraph with title formatting ''' ''' ''' Private Shared Function GetTitleParagraph(text As String, level As Integer) As NParagraph Dim color = NColor.Black Dim paragraph = GetTitleParagraphNoBorder(text, level) paragraph.HorizontalAlignment = ENAlign.Left paragraph.Border = CreateLeftTagBorder(color) paragraph.BorderThickness = defaultBorderThickness Return paragraph End Function Private Shared Function GetNoteBlock(text As String, level As Integer) As NGroupBlock Dim color = NColor.Red Dim paragraph = GetTitleParagraphNoBorder("Note", level) Dim groupBlock As NGroupBlock = New NGroupBlock() groupBlock.ClearMode = ENClearMode.All groupBlock.Blocks.Add(paragraph) groupBlock.Blocks.Add(GetDescriptionParagraph(text)) groupBlock.Border = CreateLeftTagBorder(color) groupBlock.BorderThickness = defaultBorderThickness Return groupBlock End Function Private Shared Function GetDescriptionBlock(title As String, description As String, level As Integer) As NGroupBlock Dim color = NColor.Black Dim paragraph = GetTitleParagraphNoBorder(title, level) Dim groupBlock As NGroupBlock = New NGroupBlock() groupBlock.ClearMode = ENClearMode.All groupBlock.Blocks.Add(paragraph) groupBlock.Blocks.Add(GetDescriptionParagraph(description)) groupBlock.Border = CreateLeftTagBorder(color) groupBlock.BorderThickness = defaultBorderThickness Return groupBlock End Function ''' ''' Creates a left tag border with the specified border ''' ''' ''' Private Shared Function CreateLeftTagBorder(color As NColor) As NBorder Dim border As NBorder = New NBorder() border.LeftSide = New NBorderSide() border.LeftSide.Fill = New NColorFill(color) Return border End Function Private Shared Function GetLoremIpsumParagraph() As NParagraph Return New NParagraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum placerat in tortor nec tincidunt. Sed sagittis in sem ac auctor. Donec scelerisque molestie eros, a dictum leo fringilla eu. Vivamus porta urna non ullamcorper commodo. Nulla posuere sodales pellentesque. Donec a erat et tortor viverra euismod non et erat. Donec dictum ante eu mauris porta, eget suscipit mi ultrices. Nunc convallis adipiscing ligula, non pharetra dolor egestas at. Etiam in condimentum sapien. Praesent sagittis pulvinar metus, a posuere mauris aliquam eget.") End Function ''' ''' Gets the specified text repeated ''' ''' ''' ''' Private Shared Function GetRepeatingText(text As String, count As Integer) As String Dim builder As StringBuilder = New StringBuilder() For i = 0 To count - 1 If builder.Length > 0 Then builder.Append(" ") End If builder.Append(text) Next Return builder.ToString() End Function #End Region #Region "Constants" Private Shared ReadOnly defaultBorderThickness As NMargins = New NMargins(5.0, 0.0, 0.0, 0.0) #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Header and Footer Example for WPF Solution