Find Tag & Insert PAGE Field & Finally Remove Tag from Word Document C# .NET

I am looking to replace place holders in the document body and header/footers with fields, in the case below replacing ‘[PN]’ with the PAGE field. I am using the ReplacingCallback to insert the field which works well but the text being search for is not replaced. this happens when replacing text in the body of the document as well.

var options = new FindReplaceOptions { MatchCase = false, FindWholeWordsOnly = true, ReplacingCallback = new ReplaceWithPageTotalFieldEvaluator() };
header.Range.Replace("[PT]", "", options);

IReplacingCallback implementation:

private class ReplaceWithPageFieldEvaluator : IReplacingCallback
{
	ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
	{
		DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
		builder.MoveTo(e.MatchNode);
		builder.InsertField("PAGE", "");
		e.Replacement = "";
		return ReplaceAction.Replace;
	}
}

Thanks

@pjb_maintology,

Please upgrade to the latest (21.6) version of Aspose.Words for .NET and try running the following C# code:

Document doc = new Document("C:\\Temp\\word.docx");

var options = new FindReplaceOptions
{
    MatchCase = false,
    FindWholeWordsOnly = true,
    ReplacingCallback = new ReplaceWithPageFieldEvaluator()
};

doc.Range.Replace("[PN]", "", options);

doc.Save("C:\\Temp\\output.docx");

public class ReplaceWithPageFieldEvaluator : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further removing.
        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.NextSibling;
            }
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {
            SplitRun((Run)currentNode, remainingLength);
            runs.Add(currentNode);
        }

        //// to insert new content
        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo((Run)runs[runs.Count - 1]);
        builder.InsertField("PAGE", "");

        foreach (Run run in runs)
            run.Remove();

        return ReplaceAction.Skip;
    }
    private Run SplitRun(Run run, int position)
    {
        Run afterRun = (Run)run.Clone(true);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring((0), (0) + (position));
        run.ParentNode.InsertAfter(afterRun, run);
        return afterRun;
    }
}

Thanks Awais, thats works great.

1 Like