Inserting Page Numbers

Technical Articles > Using Text Fields and Hypertext Links

In a header or footer, accessible through the HeaderFooter class, special placeholders for page numbers can be inserted. Such a placeholder is defined through the PageNumberField class, also derived from the TextField class. Using such a field, a header or footer automatically shows the current page number or the total number of pages on every page.

Page number fields inherit the editing features of the TextField class and have additionally a NumberFormat, a StartNumber and a ShowNumberOfPages property. The NumberFormat property specifies the kind of numbers used to display the page numbers. Possible values are Arabic numbers, small and large Roman numbers, letters and capital letters. The StartNumber property defines the starting page number for the first page of the section. The ShowNumberOfPages property specifies whether the field shows the page number or the total number of pages.

For each section in the document, page numbers can either be continued from the previous section or can be restarted with a certain start number. The SectionFormat class provides the SectionFormat.RestartPageNumbering property to adjust this setting.

The following code adds a header to the current section. After insertion, the header receives a text, the current page number and total number of pages fields:

HeaderFooterCollection hfc = textControl1.Sections.GetItem().HeadersAndFooters;


HeaderFooter h = hfc.GetItem(HeaderFooterType.Header);
if (h != null)
    h.Selection.Text = "Page  of ";
    h.Selection.Start = 5;
    h.Selection.Length = 0;

    PageNumberField page = new PageNumberField(1, NumberFormat.ArabicNumbers);
    page.Editable = false;
    page.DoubledInputPosition = true;

    h.Selection.Start = 9;
    h.Selection.Length = 0;

    PageNumberField totalPages = new PageNumberField();
    totalPages.Editable = false;
    totalPages.DoubledInputPosition = true;
    totalPages.ShowNumberOfPages = true;
[Visual Basic]
Dim hfc As HeaderFooterCollection = textControl1.Sections.GetItem().HeadersAndFooters


Dim h As HeaderFooter = hfc.GetItem(HeaderFooterType.Header)
If h IsNot Nothing Then
    h.Selection.Text = "Page  of "
    h.Selection.Start = 5
    h.Selection.Length = 0

    Dim page As New PageNumberField(1, NumberFormat.ArabicNumbers)
    page.Editable = False
    page.DoubledInputPosition = True

    h.Selection.Start = 9
    h.Selection.Length = 0

    Dim totalPages As New PageNumberField()
    totalPages.Editable = False
    totalPages.DoubledInputPosition = True
    totalPages.ShowNumberOfPages = True
End If

After the header has been inserted in the document, it receives the text Page  of  with a space after Page and at the end. Because a header has no Text property, this is done by replacing the text of the current text selection using the Selection class. Initially, a newly inserted header has no text, therefore the initial text selection has a length of zero so that the new text is inserted.

Afterwards, the current text input position is set between Page and of where the page number field is inserted. This field is set to not editable, so that an end-user cannot change its current text. To enable text insertion right of the page number, a second input position is needed. This can be reached by pressing the Right Arrow key at the text end.

After that, the input position is set to the end to insert the total number of pages field.