Content Control not geeting replaced

Termsheet Template - Zudio - Latest (27)erf.DOCX (30.0 KB)

private static Run SplitRun(Run run, int position)
{
    Int32 runningCounter = 0;
    try
    {
        runningCounter = 1;
        Run afterRun = (Run)run.Clone(true);
        runningCounter = 2;
        run.ParentNode.InsertAfter(afterRun, run);
        runningCounter = 3;
        afterRun.Text = run.Text.Substring(position);
        runningCounter = 4;
        run.Text = run.Text.Substring((0), (0) + (position));
        runningCounter = 5;
        return afterRun;
    }
    catch (Exception ex)
    {
    }
}

Parentnode of second control is null… it is not getting replaced by contentcontrol

@Rubal I do not see any content controls in your input document. I see text placeholders like {palceholder}. I suppose you are using Find/Replace functionality and IReplacingCallback to replace placeholders with real data. Please try using FindReplaceDirection.Backward instead of FindReplaceDirection.Forward in FindReplaceOptions.Direction and let us know if the problem still persist.

Please find my output documentTermsheet Template - Zudio - Latest (28).DOCX (64.1 KB)

Here one is getting replaced with content control and other remaining the same

Please help need it on urgent basis

@Rubal If you need to replace text placeholders in your document with content control, you can use the following code:

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

FindReplaceOptions opt = new FindReplaceOptions(FindReplaceDirection.Backward, new ReplaceEvaluatorFindAndReplaceWithContentControl());
doc.Range.Replace(new Regex(@"\{[^\{\}]+\}"), "", opt);

doc.Save(@"C:\Temp\out.docx");
internal class ReplaceEvaluatorFindAndReplaceWithContentControl : 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);
        }

        // Insert SDT.
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
        tag.Tag = e.Match.Value;
        runs[0].ParentNode.InsertBefore(tag, runs[0]);

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