How to insert HTML before specific node

I have StructuredDocumentTag as an reference that is nested with other SDTs and composite nodes. I would like to insert html content before that node.

StructuredDocumentTag sdt = new StructuredDocumentTag();
DocumentBuilder builder = new DocumentBuilder(document);
StringBuilder sb = new StringBuilder();
sb.Append("<html><body>");
sb.Append(htmlText);
sb.Append("</body></html>");
// I miss some part here - how to insert next line before current node - sdt
builder.InsertHtml(sb.ToString());

Hi Rastko,

Thanks for your inquiry. Please add a paragraph inside StructuredDocumentTag and move the cursor to this paragraph and insert the Html using DocumentBuilder.InsertHtml method. Hope this helps you. Please let us know if you have any more queries.

Thank you for your response.

In this case I will have Sdt with paragraph that contains html. I would like to insert HTML before Sdt. How to accomplish that ?

Thanks

Hi Rastko,

Thanks for your inquiry. In this case, you need to move the cursor to the node before Sdt. Please read following documentation links for your kind reference.

DocumentBuilder Overview
Moving the Cursor
Inserting Document Elements

If you face any issue, please share your input document and html snippet here for our reference. We will then provide you more information about your query along with code.

It is a problem as I can have before that Sdt other Sdt. As you know, DocumentBuilder.MoveTo fails if you move to node that is type of Sdt.
Could you suggest how to move to first sibling safely independently of node type ?

Hi Rastko,
Thanks for your inquiry.
In this case, you need to move the cursor to the first child node of SDT and insert the html. Please check following code snippet.
If you still face problem, please share your input Word document, Html and expected output document here for our reference. We will then provide you more information about your query along with code.

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(((StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true)).FirstChild);
builder.InsertHtml(
    "<P align='right'>Paragraph right</P>" +
    "<b>Implicit paragraph left</b>" +
    "<div align='center'>Div center</div>" +
    "<h1 align='left'>Heading 1 left.</h1>");