Replacing Text that has carriage return in Aspose.Words

Hello,

I am trying to get the replace text using Aspose.Words and the text I need to insert has carraoge returns. I am getting incorrect results with the code I’m using. Can you please take a look at the code and let me know what I am doing wrong? I’ve included the test project, and word documents (before and after).

Thanks!

Don Tompkins

Hi
Thanks for your request. As I can see, in your code you move DocumentBuilder cursor to the MatchNode. But, the match node is not actually text that you searched for. This is just Run that contains the searched text, this Run can contain other text before and after the searched text. Also searched text can be spanned into few Run nodes (for example in case when parts of the searched text have different formatting). So this is the reason why replacement is in the incorrect place. You should use code like shown in the following article to process such cases:
https://docs.aspose.com/words/net/find-and-replace/
Also, in your code you are using InsertHtml method. So to insert line break you should use HTML markup, i.e.
or
tags.
Hope this information could help you.
Best regards.

Hello,

The sample I sent was the best sample I could find online to try to replace text. I am unable to see how to replace text with the link you sent me. It shows how to highlight the words that are found, which will work to find the words in question, but how do I replace those words in a search and replace fashion? And primarily my search criteria may not have carriage returns, but I replace criteria may have carriage returns.

Thanks!

Don Tompkins

Hi
Thanks for your request. I create a simple code example that demonstrates the technique mentioned above:

[Test]
public void Test001()
{
    Document doc = new Document(@"Test001\in.doc");
    doc.Range.Replace(new Regex(@"test"), new ReplaceEvaluatorFindAndInsertText("Here you can insert any text\nWith special characters you want\fall of them will be properly handled"), false);
    doc.Save(@"Test001\out.doc");
}
private class ReplaceEvaluatorFindAndInsertText: IReplacingCallback
{
    public ReplaceEvaluatorFindAndInsertText(string text)
    {
        mText = text;
    }
    ///
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// This method replaces the match string, even if it spans multiple runs.
    ///
    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);
        }
        // Create Document Buidler aond insert text
        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
        builder.MoveTo((Run) runs[runs.Count - 1]);
        builder.Write(mText);
        // Now remove all runs in the sequence.
        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;
    }
    ///
    /// Splits text of the specified run into two runs.
    /// Inserts the new run just after the specified run.
    ///
    private static Run SplitRun(Run run, int position)
    {
        Run afterRun = (Run) run.Clone(true);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring(0, position);
        run.ParentNode.InsertAfter(afterRun, run);
        return afterRun;
    }
    private string mText;
}

Hope this helps. Please let me know if you need more information. I will be glad to help you.
Best regards,