Unable to replace content inside block content control

hi,

we are facing an issue with replacing content inside block content control. here is an example,

title: $$placeholder1$$ $$placeholder2$$ $$placeholder3$$ $$placeholder4$$

in the above, entire line is in a content control, and we iterate over the placeholders to replace. so when we replace the second time, the placeholders are not there. only the first one is replaced.

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
    ReplaceWith = ReplaceWith == null ? "" : ReplaceWith;
    // 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  
    var document = e.MatchNode.Document as Document;
    DocumentBuilder builder = new DocumentBuilder(document);
    var runToReplace = (Run)runs[runs.Count - 1];
    builder.MoveTo(runToReplace);

    if (ChangeToWhiteFont)
    {
        foreach (Run run in runs)
        {
            run.Font.Color = Color.White;
        }
        return ReplaceAction.Skip;
    }
    else if (IsContentControlRequired)
    {
        var tag = Searchstring.Replace("##", "").Replace("$$", "").Replace("**", "");
        StructuredDocumentTag sdtRichText = new StructuredDocumentTag(document, SdtType.RichText, IsBlockContentControl ? MarkupLevel.Block : MarkupLevel.Inline);
        sdtRichText.RemoveAllChildren();
        sdtRichText.Tag = tag;
        sdtRichText.IsShowingPlaceholderText = false;
        if (IsBlockContentControl)
        {

            Run run = new Run(document);
            run.Text = "";

            Paragraph para = new Paragraph(document);
            para.AppendChild(run);
            sdtRichText.AppendChild(para);
            // below line is causing the issue
            runToReplace.ParentNode.ParentNode.InsertAfter(sdtRichText, runToReplace.ParentNode);
            builder.MoveTo(sdtRichText.FirstChild);
            builder.InsertHtml(ReplaceWith, HtmlInsertOptions.UseBuilderFormatting);
            if (builder.CurrentParagraph != null && builder.CurrentParagraph.ToString(SaveFormat.Text).Trim() == "")
                builder.CurrentParagraph.Remove();

            // formatting the content to match the searchstring's styles
            NodeCollection runNodes = sdtRichText.GetChildNodes(NodeType.Run, true);
            foreach (Run rn in runNodes)
            {
                rn.Font.Color = runToReplace.Font.Color;
                rn.Font.Name = runToReplace.Font.Name;
                rn.Font.Style = runToReplace.Font.Style;
                rn.Font.Size = runToReplace.Font.Size;
                rn.Font.Italic = runToReplace.Font.Italic;
                rn.Font.Bold = runToReplace.Font.Bold;
                rn.Font.StrikeThrough = runToReplace.Font.StrikeThrough;
            }
            if (runToReplace.ParentNode != null)
            {
                runToReplace.ParentNode.Remove();
            }

        }
        else
        {
            runToReplace.Text = ReplaceWith;
            sdtRichText.AppendChild(runToReplace.Clone(true));
            sdtRichText.LockContentControl = true;
            sdtRichText.LockContents = true;
            builder.InsertNode(sdtRichText);
        }
    }
    else
    {
        builder.InsertHtml(ReplaceWith, HtmlInsertOptions.UseBuilderFormatting);
        if (builder.CurrentParagraph != null && Searchstring.Contains(builder.CurrentParagraph.ToString(SaveFormat.Text).Trim()))
            builder.CurrentParagraph.Remove();
    }

    foreach (Run run in runs)
    {
        run.Remove();
    }

    return ReplaceAction.Skip;
}

one workaround we tried, is having a paragraph break between each placeholder but we want the title and placeholders to be in same line

another workaround we tried is, if the line has multiple placeholders, we switch it to inline content control

please advise, thank you

@randomuser123 can you please attach a sample of the input and the expected output. Also, the provided code uses methods and properties not available, can you please create a simple console application with your code, zip it and post to this thread.

aspose.zip (300.8 KB)

hi eduardo,

i have attached the zip file and the expected output file.
in the expected output file, we want the html to be replaced as executed html instead of with the tags

this is the error we are facing when all the lookupstrings to be replaced are on the same line

image.png (19.5 KB)

Hi @eduardo.canal
the problem here is-
We have a paragraph with some text and a placeholder and want to replace the the placeholder with a content control, preferably block content control as we are inserting HTML file inside the content control.

Example: Input : Customer details: [placeholder]
Expected Output:

@randomuser123 Thank you for provide additional information. We are checking the attached files and working on the issue and will get back to you as soon as possible.

@randomuser123 I modified a bit the solution that you provided, now it´s working as expected. Please see the attached project.
aspose.zip (321.8 KB)

Hi @eduardo.canal thank you for your response. unfortunately that does not solve our issue of block content control inside a paragraph. the generated document does not have content controls. please advise

@randomuser123 sorry I used the output document that you uploaded as reference. I going to start to refactor the solution using your comments, but will be helpful if you update the output file as well.

hi @eduardo.canal

expectedoutput.docx (23.7 KB)

i have uploaded the expected output as docx for your reference, please review and advise

1 Like

@randomuser123 I updated the solution to fit with your needs. Please check it a post if you have any doubt.
aspose.zip (269.2 KB)

hi eduardo, this is working fine, thanks for your help :slight_smile:

2 Likes