Remvoe structred control and add text- Cannot insert a node of this type at this location

template.docx (18.4 KB)
Hi Team,
I need to remove structured control and replace text. I used below code

sdt is StructuredDocumentTag

Aspose.Words.CompositeNode parent = sdt.ParentNode;
Run run1 = new Run(returndocument, _controlName);

try
  {
     parent.InsertBefore(run1, sdt); ---throws exceptions at this point
     sdt.Remove();
  }
catch (Exception ex)
  {
  }

Exception Message = Cannot insert a node of this type at this location.

@Vipin_Paliwal SDT in your document is block level, i.e. it can contain only block level nodes, like paragraph or table. Run on other hand is inline level node so it cannot be inserted on the same level as block level nodes. In your case you should insert a paragraph:

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

StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);

Paragraph para = new Paragraph(doc);
para.AppendChild(new Run(doc, "This is my Run"));
sdt.ParentNode.InsertBefore(para, sdt);
sdt.Remove();

doc.Save(@"C:\Temp\out.docx");