Insert content inside a Content Control using Aspose.Words

Hi there,

my company is using Aspose.Words and we now have a new need using Aspose.Words. So we have a Word doc ( as a Template for a report ), and inside this Word Doc we have several content controls. I want to be able to programmatically find this content controls and insert content in each one of this. So i would like to ask for a sample explaining how to do it, since i tried to google for some sources and i didn’t find anything that explains the solution for this need.

Thank you,
HB

Hi

Thanks for your inquiry. Sure, you can interact with content controls by using StructuredDocumentTag class of Aspose.Words. For example, here is the code to insert plain text in the content control (also see attached SDT.docx):

Document doc = new Document(MyDir + @"SDT.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    if (sdt.SdtType == SdtType.PlainText)
    {
        sdt.RemoveAllChildren();
        Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph;
        Run run = new Run(doc, "new text goes here");
        para.AppendChild(run);
    }
}
doc.Save(MyDir + @"16.1.0.docx");

Hope, this helps.

Best regards,