Inserting a bookmark after a specified text

Hello,

I am trying to insert a bookmark right after a specified text (lets say “toto”) but so far i am only able to find all the nodes that contain my specified text and insert my bookmark right before it.

How can i do that ?

Sincerely.

@guyyyyyyyyyyy You can achieve this using Find and Replace functionality and IReplacingCallback. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions(FindReplaceDirection.Backward);
opt.ReplacingCallback = new ReplaceEvaluatorWrapWithBookmark();
doc.Range.Replace("test", "bookmakr_name", opt);
doc.Save(@"C:\Temp\out.docx");
internal class ReplaceEvaluatorWrapWithBookmark : 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;

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

        // Generate an unique bookmakr name. Another approach can be used.
        string bookmarkName = e.Replacement;
        while (doc.Range.Bookmarks[bookmarkName] != null)
            bookmarkName += "_" + Guid.NewGuid().ToString();

        // Insert a bookmakr around the matched text
        BookmarkStart start = new BookmarkStart(doc, bookmarkName);
        BookmarkEnd end = new BookmarkEnd(doc, bookmarkName);

        runs[0].ParentNode.InsertBefore(start, runs[0]);
        runs[runs.Count-1].ParentNode.InsertAfter(end, runs[runs.Count-1]);

        // 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;
    }
}

The code wraps each occurrence of "test" word into a bookmark.

thanks for your answer.

I had to modify the code
runs[0].ParentNode.InsertBefore(start, runs[0]);
into
runs[runs.Count - 1].ParentNode.InsertAfter(end, runs[runs.Count - 1]);

In order to insert my bookmark right after the matched text (instead of wraping it with my bookmark) but otherwise it works !

@guyyyyyyyyyyy Yes, if you need to insert a bookmark after the matched text, you should use the following code:

runs[runs.Count-1].ParentNode.InsertAfter(end, runs[runs.Count-1]);
runs[runs.Count-1].ParentNode.InsertAfter(start, runs[runs.Count-1]);

Indeed i also had to insert the BookmarkEnd before the BookmarkStart since the insertion is done right after the node.

@guyyyyyyyyyyy Alternatively you can insert end after the start, like this:

runs[runs.Count - 1].ParentNode.InsertAfter(start, runs[runs.Count - 1]);
start.ParentNode.InsertAfter(end, start);