Adding a Header and a Footer with Data to a Document

To insert and access headers and footers in a document, the SectionCollection.GetItem method must be used. The following code sample explains how to add a header and a footer to a document. After that, text and an image will be placed in the header and a page number will be added to the footer.

To begin with, it must be checked if there are already a header and footer in the current section. If not, add a header and footer and add content to both. Please note that a dialog will pop up to ask you for the image file. You can of course provide an Image object which will be inserted without any file dialog.

[C#]
// Get the current section to work with.
TXTextControl.Section currentSection = textControl1.Sections.GetItem();

// Get the current header.
TXTextControl.HeaderFooter currentHeader =
      currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header);

// Check if there is already a header present.
if (currentHeader == null)
{ // no header found in the current section, so we will add one.
  currentSection.HeadersAndFooters.Add(TXTextControl.HeaderFooterType.Header);
  currentHeader = currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header);
}

// Add image to the header with alignment to the right page border.
currentHeader.Images.Add(new TXTextControl.Image(),
                TXTextControl.HorizontalAlignment.Right,
                0,
                TXTextControl.ImageInsertionMode.DisplaceText);

// Add some text to the header, left aligned.
currentHeader.Selection.Text = "This is a header with an image aligned to the right.";

TXTextControl.HeaderFooter currentFooter =
      currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Footer);
if (currentFooter == null)
{
  currentSection.HeadersAndFooters.Add(TXTextControl.HeaderFooterType.Footer);
  currentFooter = currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Footer);
}
currentFooter.Selection.Text = "Page ";

// Add PageNumberField text field to the footer to display the current page number.
currentFooter.PageNumberFields.Add(new TXTextControl.PageNumberField(1, TXTextControl.NumberFormat.ArabicNumbers));

// Add static page count to the footer.
// PLEASE NOTE: You will have to update this number programmatically if the page count changes!
// Best solution would be to do that in the Changed event.
currentFooter.Selection.Text = " of " + textControl1.Pages.ToString();

// Center the footer content.
currentFooter.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center;
[Visual Basic]
' Get the current section to work with.
Dim currentSection As TXTextControl.Section = TextControl1.Sections.GetItem()

' Get the current header.
Dim currentHeader As TXTextControl.HeaderFooter =
      currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header)

' Check if there is already a header present.
If currentHeader Is Nothing Then
' no header found in the current section, so we will add one.
  currentSection.HeadersAndFooters.Add(TXTextControl.HeaderFooterType.Header)
  currentHeader = currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header)
End If

' Add image to the header with alignment to the right page border.
                currentHeader.Images.Add(New TXTextControl.Image(), _
                TXTextControl.HorizontalAlignment.Right, _
                0, _
                TXTextControl.ImageInsertionMode.DisplaceText)

' Add some text to the header, left aligned.
currentHeader.Selection.Text = "This is a header with an image aligned to the right."

Dim currentFooter As TXTextControl.HeaderFooter =
      currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Footer)
If currentFooter Is Nothing Then
  currentSection.HeadersAndFooters.Add(TXTextControl.HeaderFooterType.Footer)
  currentFooter = currentSection.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Footer)
End If
currentFooter.Selection.Text = "Page "

' Add PageNumberField text field to the footer to display the current page number.
currentFooter.PageNumberFields.Add(New TXTextControl.PageNumberField(1, TXTextControl.NumberFormat.ArabicNumbers))

' Add static page count to the footer.
' PLEASE NOTE: You will have to update this number programmatically if the page count changes!
'Best solution would be to do that in the Changed event.
currentFooter.Selection.Text = " of " & TextControl1.Pages.ToString()

' Center the footer content.
currentFooter.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center

As noted in the source code, the total page number textControl1.Pages.ToString() must be updated each time the number of pages has changed.

<< Back