Add Content Controls to Microsoft Office 2016 Word in C# Introduction

Sometimes we may need to have a user interface in Microsoft Office Word. Today we discuss how we can add content controls like drop down list, check box, etc. to Microsoft Office 2016 Word using Spire.Doc controls.

Open Microsoft Visual Studio 2015, Create Console Application and name it as CSharpMSWord. Install Spire.Doc using Nuget. To install content controls, got to Tools => NuGet Package Manager => Package Manager Console as shown below.

Execute “Install-Package FreeSpire.Doc” command as shown below.

Once Spire.Doc components successfully installed, add below code to Program class.

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

using System; 

namespace CSharpMSWord

{

    class Program

    {

        static void Main(string[] args)

        {

            Document msDocument = new Document();

            Section contentSection = msDocument.AddSection();

            Paragraph paragraph = contentSection.AddParagraph();           

            StructureDocumentTagInline sdt = new StructureDocumentTagInline(msDocument);

            paragraph.ChildObjects.Add(sdt);

            sdt.SDTProperties.SDTType = SdtType.DropDownList;

            sdt.SDTProperties.Alias = "drop-down list";

            sdt.SDTProperties.Tag = "drop-down list";

            SdtDropDownList sdMonths = new SdtDropDownList();

            sdMonths.ListItems.Add(new SdtListItem("January", "1"));

            sdMonths.ListItems.Add(new SdtListItem("February", "2"));

            sdMonths.ListItems.Add(new SdtListItem("March", "3"));

            sdMonths.ListItems.Add(new SdtListItem("April", "4"));

            sdt.SDTProperties.ControlProperties = sdMonths; 

            TextRange textRange = new TextRange(msDocument);

            textRange.Text = sdMonths.ListItems[0].DisplayText;

            sdt.SDTContent.ChildObjects.Add(textRange);

            paragraph = contentSection.AddParagraph();

            sdt = new StructureDocumentTagInline(msDocument);

            paragraph.ChildObjects.Add(sdt); 

            sdt.SDTProperties.SDTType = SdtType.Text;

            sdt.SDTProperties.Alias = "Text Only";

            sdt.SDTProperties.Tag = "Text Only";           

            textRange = new TextRange(msDocument);

            textRange.Text = "Enter text here.";

            sdt.SDTContent.ChildObjects.Add(textRange); 

            paragraph = contentSection.AddParagraph();

            sdt = new StructureDocumentTagInline(msDocument);

            paragraph.ChildObjects.Add(sdt);

            sdt.SDTProperties.SDTType = SdtType.Checkbox;

            sdt.SDTProperties.Alias = "CheckBox Control";

            sdt.SDTProperties.Tag = "CheckBox Control";

            textRange = new TextRange(msDocument);

            textRange.Text = "Select Check Box";

            sdt.SDTContent.ChildObjects.Add(textRange); 

            paragraph = contentSection.AddParagraph();

            sdt = new StructureDocumentTagInline(msDocument);

            paragraph.ChildObjects.Add(sdt);

            sdt.SDTProperties.SDTType = SdtType.RichText;

            sdt.SDTProperties.Alias = "Rich text box";

            sdt.SDTProperties.Tag = "Rich text box";

            textRange = new TextRange(msDocument);

            textRange.Text = "Insert formatted text here.";

            sdt.SDTContent.ChildObjects.Add(textRange);            

            paragraph = contentSection.AddParagraph();

            sdt = new StructureDocumentTagInline(msDocument);

            paragraph.ChildObjects.Add(sdt);

            sdt.SDTProperties.SDTType = SdtType.DatePicker;

            sdt.SDTProperties.Alias = "Date picker control";

            sdt.SDTProperties.Tag = "Date picker control";

            SdtDate date = new SdtDate();

            date.CalendarType = CalendarType.Default;

            date.DateFormat = "yyyy.MM.dd";

            date.FullDate = DateTime.Now;

            sdt.SDTProperties.ControlProperties = date;

            textRange = new TextRange(msDocument);

            textRange.Text = "1983.08.13";

            sdt.SDTContent.ChildObjects.Add(textRange);            

            paragraph = contentSection.AddParagraph();

            sdt = new StructureDocumentTagInline(msDocument);

            paragraph.ChildObjects.Add(sdt);

            sdt.SDTProperties.SDTType = SdtType.ComboBox;

            sdt.SDTProperties.Alias = "Combo box Control";

            sdt.SDTProperties.Tag = "Combo box Control";

            SdtComboBox cb = new SdtComboBox();

            cb.ListItems.Add(new SdtListItem("US", "1"));

            cb.ListItems.Add(new SdtListItem("UK", "2"));

            cb.ListItems.Add(new SdtListItem("India", "3"));

            sdt.SDTProperties.ControlProperties = cb;

            textRange = new TextRange(msDocument);

            textRange.Text = cb.ListItems[0].DisplayText;

            sdt.SDTContent.ChildObjects.Add(textRange); 

            paragraph = contentSection.AddParagraph();

            sdt = new StructureDocumentTagInline(msDocument);

            paragraph.ChildObjects.Add(sdt);

            sdt.SDTProperties.SDTType = SdtType.Picture;

            sdt.SDTProperties.Alias = "Picture Control";

            sdt.SDTProperties.Tag = "Picture Control"; 

            //Save the Microsoft Office 2016 Wordfile    

            msDocument.SaveToFile(@"E:\Documents\msword2016.docx", FileFormat.Docx);

        }

    }

} 

Run the application; it saves the Microsoft Office 2016 Word document in E:\Documents folder. Open the document, and it displays controls as shown below.

                                                                                               CSharpMSWord.zip