We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to insert a content control to word document

I have a word document with many content controls .
How I can search a content control by name and insert another content control next to existing one
Please let me know how to do it in Aspose.cells c#


Thanks for your inquiry. You can get the content controls from the document using CompositeNode.GetChildNodes method as shown in following code snippet. To identify the content control, you can use StructuredDocumentTag.Tag or StructuredDocumentTag.Title properties.

Document doc = new Document(MyDir + @"in.docx");
foreach (StructuredDocumentTag structuredDocumentTag in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
    if (structuredDocumentTag.Tag == "tagname")
        //your code...

We suggest you please read the following article. Hope this helps you.
Working with Content Control SDT

Hi @tahir.manzoor
Thanks for replay .I have attached two word documents for your reference .If you see in TestDocument1.docx there are 4 content controls (Richtext1,RichText2,RichText3 and RichText4)
and in TestDocument2.docx there are 3 content controls where RichText2 has been deleted .
My question is in Aspose how can I compare and insert RichText2 after RichText1 only in TestDocument1.docx
ThanksSampleDocuments.zip (32.2 KB)


Thanks for sharing the detail. Please use the following code example to insert the content control after “Rich Text2”. Hope this helps you.

Document doc = new Document(MyDir + @"TestDocument1.docx");
foreach (StructuredDocumentTag structuredDocumentTag in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
    if (structuredDocumentTag.Tag == "RichText2")
        StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
        Paragraph para = new Paragraph(doc);
        Run run = new Run(doc);
        run.Text = "Rich Text 2";
        run.Font.Color = Color.Green;

        structuredDocumentTag.ParentNode.InsertAfter(sdt, structuredDocumentTag);
doc.Save(MyDir + "output.docx");

Could you please share some more detail about comparing contents? If you want to compare both documents, please refer to the following article.
How to Compare Two Word Documents

Hi @ [tahir.manzoor]
Thanks for your replay .
I want to insert “Richtext2” content control in TestDocument2.docx after copying it from testDocument1.docx. I dont want to create a new object of StructuredDocumentTag
When I am trying to do so in Aspose getting exception as “The newChild was created from a different document than the one that created this node.”
Could you please help me to do so .
My requirement is looping both the document and checking mising content control , this this case “Richtext2” , then I can copy the same from existing document and insert in missing document in right place


Thanks for your inquiry. The NodeImporter class allows to efficiently perform repeated import of nodes from one document to another. You can use NodeImporter.ImportNode method to import a node from one document into another.

Following code example shows how to import content control from one document into another. Hope this helps you.

Document doc1 = new Document(MyDir + @"TestDocument1.docx");
Document doc2 = new Document(MyDir + @"TestDocument2.docx");

NodeImporter imp = new NodeImporter(doc1, doc2, ImportFormatMode.KeepSourceFormatting); 

//Get the "Rich text2" from first document. 
foreach (StructuredDocumentTag structuredDocumentTag in doc1.GetChildNodes(NodeType.StructuredDocumentTag, true))
    if (structuredDocumentTag.Tag == "RichText2")
        //Get content control from second documnt. 
        StructuredDocumentTag sdt = (StructuredDocumentTag)doc2.GetChild(NodeType.StructuredDocumentTag, 0, true);

        Node impNode = imp.ImportNode(structuredDocumentTag, true);
        sdt.ParentNode.InsertAfter(impNode, sdt);
doc2.Save(MyDir + "output.docx");