//Create new paragraph
WParagraph paragraph = new WParagraph(footer.Document);
//To get the total number of pages excluding first page, the field should be in below syntax
//{={NUMPAGES}-1}
//Insert the formula field to the paragraph
WField formulafield = paragraph.AppendField("Formula", FieldType.FieldExpression) as WField;
formulafield.FieldCode = "=";
//Get the index of the formula field in the paragraph items.
int fieldindex = paragraph.ChildEntities.IndexOf(formulafield) + 1;
//Create a temporary paragraph
WParagraph tempParagraph = new WParagraph(footer.Document);
//Append NUMPAGES field
tempParagraph.AppendField("", FieldType.FieldNumPages);
for (int i = 0; i < tempParagraph.ChildEntities.Count; i++)
{
//Get the NUMPAGES field from the temporary paragraph and insert it as first operand in the formula field paragraph.
if (tempParagraph.ChildEntities[i] is WField &&
(tempParagraph.ChildEntities[i] as WField).FieldType == FieldType.FieldNumPages)
{
//The NUMPAGES field is inserted at the index next to Formula field in the paragraph
int itemsCount = paragraph.ChildEntities.Count;
paragraph.ChildEntities.Insert(fieldindex, tempParagraph.ChildEntities[i]);
fieldindex += paragraph.ChildEntities.Count - itemsCount;
break;
}
}
IWTextRange text = new WTextRange(footer.Document);
//Insert the operater and second operand to the formula field
text.Text = " - " + 1;
paragraph.ChildEntities.Insert(fieldindex, text);
//Add the page number paragraph to the required footer in the document
footer.ChildEntities.Add(paragraph); |