Insert a buildingblock right after a specific run

Hello,

Right now i am able to search for a run that is at the end of a specific text and i wanted to insert a buildingblock rigth after this node.
Since i don’t want to add a new section to my document, i’m proceeding as following:

DocumentBuilder builder = new DocumentBuilder(pDocument);
builder.MoveTo(MyLastRun); // MyLastRun is a run corresponding to the end of a specific text
foreach (Section section in block.Sections)
{
    foreach (var elem in importedSection.Body.ChildNodes)
    {
        builder.CurrentParagraph.ParentNode.InsertAfter(elem, builder.CurrentParagraph);
    }
}

This works almost as i want, the only issue is that it creates a line feed so the final result looks like

Instead of

Do you have any idea on how to proceed ?

As you can see, the buildingblock contains a shape but it may include some text.
Also if needed we can consider the specific text to correspond to a whole paragraph.

Sincerely.

@guyyyyyyyyyyy This occurs because you elem in your case is a block level node, i.e. either a paragraph or a table. So it is expected that there is a paragraph break. This code builder.CurrentParagraph.ParentNode.InsertAfter inserts the node after the paragraph.
In your case if the building block consists of only one paragraph, you should copy content of this paragraph to the current paragraph.

1 Like

Thanks for your answer,

Indeed it works if i do the following treatment:

if (elem.NodeType == NodeType.Paragraph)
{
    foreach (var subElem in ((Paragraph)elem).ChildNodes)
        builder.CurrentParagraph.AppendChild(subElem);
}

In case i should insert a buildingblock that is not only composed of block level nodes, is there a general way to do this insertion ?

@guyyyyyyyyyyy Unfortunately, there is no general method. A building block actually can consist of any types of nodes, so it is required to use different approaches for different types of nodes and depending n your requirements.