We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace method

When we replace any thing in word, we see the new text as inserted and then the previous text as crossed.
If I want to reverse the order. is there a way/Functionality in aspose to do it.
I want the output similar to image “after.png”
see the attachment for better understanding.

@cacglo Currently, there is no built in way to achieve this. I have logged this issue as WORDSNET-23740. We will investigate the issue and let you know whether Aspose.Words can mimic MS Word behavior in this scenario.
As a temporary workaround you can use IReplacingCallback to achieve what you need. For example see the following code:

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

doc.StartTrackRevisions("aspose");

FindReplaceOptions options = new FindReplaceOptions();
options.Direction = FindReplaceDirection.Backward;
options.ReplacingCallback = new ReplaceEvaluatorFindAndReplace();
doc.Range.Replace("Aspose", "aSPOSE", options);

doc.StopTrackRevisions();

doc.Save(@"C:\Temp\out.docx");
private class ReplaceEvaluatorFindAndReplace : IReplacingCallback
{
    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Document doc = (Document)e.MatchNode.Document;
        // Stop tracking here
        doc.StopTrackRevisions();

        // 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 deleting.
        List<Run> runs = new List<Run>();

        // 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((Run)currentNode);
            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((Run)currentNode);
        }

        // Clone the run from the match and put the replacement text in it.
        Run lastRun = runs[runs.Count - 1];
        Run replacement = (Run)lastRun.Clone(true);

        // Put the replacement run right after matched runs.
        lastRun.ParentNode.InsertAfter(replacement, lastRun);
        replacement.Text = "";

        // Resume tracking
        doc.StartTrackRevisions("test");
        replacement.Text = e.Replacement;

        // Delete matched runs
        foreach (Run run in runs)
            run.Remove();

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;
    }
}
private static Run SplitRun(Run run, int position)
{
    Run afterRun = (Run)run.Clone(true);
    run.ParentNode.InsertAfter(afterRun, run);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring((0), (0) + (position));
    return afterRun;
}