Listing is not continued to next item

hi,

we have a document which has bullet points and some points have placeholders to be replaced. when we replace them, the list is not continued to next item, it is entered outside list.

aspose test.docx (21.0 KB)
expected output.docx (27.4 KB)
sample output.docx (21.3 KB)

our replacement code

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);
    var lockContents = IsBlockContentControl == false;
    if (ChangeToWhiteFont)
    {
        foreach (Run run in runs)
        {
            run.Font.Color = Color.White;
        }
        return ReplaceAction.Skip;
    }
    else if (IsContentControlRequired)
    {
        var tag = Searchstring.Replace("##", "").Replace("$$", "").Replace("**", "");

        if (!IsBlockContentControl)
        {
            ReplaceWith = string.IsNullOrWhiteSpace(ReplaceWith) ? $"[{tag} will be populated from system data.]" : ReplaceWith;
        }
        // if lookup string is in paragraph with some other text or lookstrings, use inline content control
        if (IsBlockContentControl && (runToReplace.ParentNode.ToString(SaveFormat.Text).Trim() != Searchstring || runToReplace.ParentNode.GetChildNodes(NodeType.Shape, true).Count > 0))
        {
            IsBlockContentControl = false;
            FormattedText = true;
        }

        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);
            para.ParagraphFormat.Alignment = runToReplace.ParentParagraph.ParagraphFormat.Alignment;
            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();

            if (runToReplace.ParentNode != null)
            {
                runToReplace.ParentNode.Remove();
            }
        }
        else
        {
            if (FormattedText)
            {
                // Create a temporary document with formatted text and import that into the content control
                Document tempDoc = (Document)document.Clone(false);
                tempDoc.RemoveAllChildren();
                tempDoc.EnsureMinimum();
                DocumentBuilder tempBuilder = new DocumentBuilder(tempDoc);
                tempBuilder.InsertHtml(ReplaceWith);

                NodeImporter importer = new NodeImporter(tempDoc, document, ImportFormatMode.KeepSourceFormatting);

                foreach (Section srcSection in tempDoc.Sections.OfType<Section>())
                {
                    foreach (Node srcNode in srcSection.Body)
                    {
                        if (((Aspose.Words.CompositeNode)srcNode)?.Count > 0)
                        {
                            NodeCollection nodesToBeInserted = ((Aspose.Words.CompositeNode)srcNode).ChildNodes;
                            foreach (Node nodeToBeInserted in nodesToBeInserted)
                            {
                                Node importNode = importer.ImportNode(nodeToBeInserted, true);
                                if (sdtRichText.LastChild == null)
                                {
                                    sdtRichText.InsertBefore(importNode, sdtRichText.LastChild);
                                }
                                else
                                {
                                    sdtRichText.InsertAfter(importNode, sdtRichText.LastChild);
                                }

                                //if (nodeToBeInserted?.NodeType == NodeType.Paragraph)
                                //{
                                //    Paragraph insertedParagraph = (Paragraph)nodeToBeInserted;
                                //    if (insertedParagraph.IsEndOfSection && !insertedParagraph.HasChildNodes)
                                //        continue;

                                //    foreach (Run run in insertedParagraph.Runs)
                                //    {
                                //        Node importNode = importer.ImportNode(run, true);
                                //        sdtRichText.InsertBefore(importNode, sdtRichText.LastChild);
                                //    }
                                //}
                                //else if (nodeToBeInserted?.NodeType == NodeType.Shape || nodeToBeInserted?.NodeType == NodeType.Run)
                                //{
                                //    Node importNode = importer.ImportNode(nodeToBeInserted, true);
                                //    sdtRichText.InsertBefore(importNode, sdtRichText.LastChild);
                                //}
                            }
                        }
                    }
                }
            }
            else
            {
                runToReplace.Text = ReplaceWith;
                sdtRichText.AppendChild(runToReplace.Clone(true));
            }
            builder.InsertNode(sdtRichText);
        }

        // 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;
            if (runToReplace.Font.Italic)
            {
                rn.Font.Italic = runToReplace.Font.Italic;
            }
            if (runToReplace.Font.Bold)
            {
                rn.Font.Bold = runToReplace.Font.Bold;
            }
            if (runToReplace.Font.StrikeThrough)
            {
                rn.Font.StrikeThrough = runToReplace.Font.StrikeThrough;
            }
            if (runToReplace.Font.Underline != Underline.None)
            {
                rn.Font.Underline = runToReplace.Font.Underline;
            }
        }

        sdtRichText.LockContentControl = lockContents;
        sdtRichText.LockContents = lockContents;
    }
    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;
}

@randomuser123 In your code you use DocumentBuilder.InsertHtml method. When you use this method by default formatting from the HTML string is used to format the value. So for example if HTML string look like this:

<p>my cool HTML</p>

it will be inserted as a paragraph without numbering even if numbering is applied to the paragraph where DocumentBuilder’s cursor is located. You can use another overload of DocumentBuilder.InsertHtml method, like this:

builder.InsertHtml("<p>my cool HTML</p>", true);

or like this:

builder.InsertHtml("<p>my cool HTML</p>", HtmlInsertOptions.UseBuilderFormatting);

In this case the inserted content will use the current DocumentBuilder’s formatting and numbering will be preserved. I see you use this overload in your code in one place, but in other places the default overload is used.
If the problem still persist, please attach your HTML string.

Hi @alexey.noskov

thanks for your response, we do have the fix you suggested but it is inside the isBlockContentControl, but we cannot use that since we might have multiple placeholders like this in the same line and converting to inline is the only solution we found to be working for our scenario

@randomuser123 You can add a condition to check whether the first run of the placeholder is the first child of the paragraph and the last run of the placeholder is the last child of the paragraph. In this case you can be sure the placeholder is the only placeholder in the paragraph:

Hi Alexey,

what is the application you are using to traverse the document?

@randomuser123 I use DocumentExplorer demo application for exploring document structure. You can get it from our Github:
https://github.com/aspose-words/Aspose.Words-for-.NET/tree/master/Examples/DocsExamples/DocumentExplorer