MS Word offers a special field type that represents the total number of pages and the current page number. These fields can be inserted into a header or footer part of the document. TX Text Control supports both types of fields, but in two different collections.
The current page number field is represented by the PageNumberField class of the HeaderFooter class and can be inserted using the code below:
TXTextControl.PageNumberField pageNumberField =
new TXTextControl.PageNumberField(1,
TXTextControl.NumberFormat.ArabicNumbers);
TXTextControl.HeaderFooter newHeader =
textControl1.Sections.GetItem().HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header);
newHeader.PageNumberFields.Add(pageNumberField);The total number of pages field is represented by the ApplicationFields collection that holds all MS Word specific fields. The logic and functionality of these fields must be handled separately. This is described in the documentation at:
Importing Fields from Microsoft Word
The page number field will be updated automatically. To update the MS Word total number of pages field, it is required to fill the field's text property with the current number of pages that can be retrieved using the Pages property of TX Text Control. The code below shows how to iterate through all fields in every header and footer to update these fields with the appropriate value.
private void updateNumPagesField()
{
// iterate through all sections
foreach (TXTextControl.Section curSection in textControl1.Sections)
{
// iterate through all headers and footers of the sections
foreach (TXTextControl.HeaderFooter curHeader in curSection.HeadersAndFooters)
{
// iterate through all application fields
foreach (TXTextControl.ApplicationField curField in curHeader.ApplicationFields)
{
// if NUMPAGES is found, update the text property
// with the current number of pages
if (curField.TypeName == "NUMPAGES")
{
curField.Text = textControl1.Pages.ToString();
}
}
}
}
}The minimum requirements for this sample application are TX Text Control .NET trial version and Visual Studio .NET 2005.