How to Replace Fields(NumPages with SectionPages) in Footer

Hello - I’m looking for a way to replace the ‘NumPages’ field with the ‘SectionPages’ field. I’ve had some success removing the ‘NumPages’ field. I can also add the ‘SectionPages’ field quite easily. However, I’m having trouble inserting the new field in the correct position on the page. Any help that anyone could provide would be appreciated. Thanks.

Try the following code:

foreach (FieldStart fieldStart in doc.GetChildNodes(NodeType.FieldStart, true))

{

Run run = fieldStart.NextSibling as Run;

if (run!= null && run.Text.Trim() == "NUMPAGES")

run.Text = " SECTIONPAGES ";

}

Hope this helps,

Hi - Thanks for the quick response to this post. Unfortunately, I implemented the code and it did not work. It appears that 'run.Text' was appropriately updated. However, the word document is still displaying the page numbers as if it were still using the 'NUMPAGES' field. Also, if I run the code below after the 'run.Text' field is updated it will still execute when looking for the 'FieldNumPages' FieldType. Is it possible to change the FieldType? Again - Any help is greatly appreciated. Thanks.

//...
//get all FieldStart nodes in a document
NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);

//iterate through document field starts collection
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType == FieldType.FieldNumPages)
{
Node node = fieldStart;
while(node.NodeType != NodeType.FieldEnd)
{
//do something in code
node = node.NextSibling;
}
}
}
//...

Hi,

There's no way to alter FieldType, and there is no necessity actually. Instead, try the following approach:

[TestMethod]

public void TestReplaceFields()

{

Document doc = new Document(@"D:\TestReplaceFields.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

NodeList fields = doc.SelectNodes("//FieldStart");

foreach (FieldStart fieldStart in fields)

{

if (fieldStart.FieldType == FieldType.FieldNumPages)

{

if (fieldStart.PreviousSibling != null)

builder.MoveTo(fieldStart.PreviousSibling);

RemoveField(fieldStart);

builder.InsertField(@"SECTIONPAGES \* MERGEFORMAT", "");

}

}

doc.Save(@"D:\TestReplaceFields Out.doc");

}

private void RemoveField(FieldStart fieldStart)

{

Node node;

Node nextNode;

for (node = fieldStart; (node != null) && (node.NodeType != NodeType.FieldEnd); node = nextNode)

{

nextNode = node.NextSibling;

node.Remove();

}

if (node != null)

node.Remove();

}

Please tell me if this helps.

Please note that to make the field show the correct value, you need to update it in MS Word, because Aspose.Words is not able to update page numbering fields yet. That is probably why your document shows the old value after running the code.

The type of the field in Aspose.Words model is defined on document load. So when you change the text of the field code it does not update the field type. But the field is written correctly to the document when saving.

Best regards,

Hi - The second code worked! Thank you both for your help with this matter.