Find Text and replace with Aother Text and Create Content control next to text

Hi team,
Test.docx (35.2 KB)
I need to find text which between {} and remove and create Content control in this location.

Can you give me some code sample. How to find text and create content control in this location.

@Vipin_Paliwal You can easily achieve this using IReplacingCallback. Please see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new ReplaceEvaluatorContentControl();
doc.Range.Replace(new Regex(@"\{[^\{\}]+\}"), "", options);
doc.Save(@"C:\Temp\out.docx");
private class ReplaceEvaluatorContentControl : IReplacingCallback
{
    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// This method replaces the match string with a StructuredDocumenttag.
    /// </summary>
    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 highlighting.
        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);
        }

        // Create SDT.
        StructuredDocumentTag sdt = new StructuredDocumentTag(e.MatchNode.Document, SdtType.RichText, MarkupLevel.Inline);
        sdt.Tag = e.Match.Value;
        sdt.Title = e.Match.Value;

        runs[0].ParentNode.InsertBefore(sdt, runs[0]);

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

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

Thanks for you reply.

1 Like

Hi Team,

Can you share code to add Custom XML mapping with Content Control and append Custom xml.

@Vipin_Paliwal You can find an example how to bind content control to custom XML parts in our documentation.