How to Add Content Controls to Word documents in C#

Content controls are Microsoft Word entities that act as containers for specific content such as dates, lists, or paragraphs of formatted text. With the help of content controls, we can easily create structured templates, forms or documents by specifying the kind of content (for example, a date, or text), restricting or allowing editing of the content etc. in a word document.

In this article, we will introduce how to add Combo Box, Text, Date Picker and Drop-Down List content controls to a word document in C# using Spire.Doc for .NET.

Here we only explained how to add a Combo Box content control to a word document in detailed steps, full codes are demonstrated at the end of this article.

Add Combo Box content control

Detail steps and code snippets:

Step 1: Create a new word document, add a section to the document and a paragraph to the section.

Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();

Step 2: Use the StructureDocumentTagInline class to create a content control, call Paragraph.ChildObjects.Add () method to add the content control to the paragraph and specify its SDT type to Combo box.

StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.ComboBox;

Step 3: Use the SdtComboBox class to create a combo box, add the items that are used for selecting to the combo box, assign the combo box to the content control, then set the display text.

SdtComboBox cb = new SdtComboBox();
cb.ListItems.Add(new SdtListItem("Cat"));
cb.ListItems.Add(new SdtListItem("Dog"));
sd.SDTProperties.ControlProperties = cb;
TextRange rt = new TextRange(document);
rt.Text = cb.ListItems[0].DisplayText;
sd.SDTContent.ChildObjects.Add(rt);

Step 4: Save and launch the file.

string resultfile = "sample.docx";
document.SaveToFile(resultfile, FileFormat.Docx);
System.Diagnostics.Process.Start(resultfile);

Below is the result word document after adding a Combo Box Content Control:

How to Add Content Controls to Word documents in C#

Full codes:

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Add_content_controls_to_word_documents
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creat a new word document
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Add Combo Box Content Control
            StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.ComboBox;
            SdtComboBox cb = new SdtComboBox();
            cb.ListItems.Add(new SdtListItem("Cat"));
            cb.ListItems.Add(new SdtListItem("Dog"));
            sd.SDTProperties.ControlProperties = cb;
            TextRange rt = new TextRange(document);
            rt.Text = cb.ListItems[0].DisplayText;
            sd.SDTContent.ChildObjects.Add(rt);

            //Add Text Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.Text;
            SdtText text = new SdtText(true);
            text.IsMultiline = true;
            sd.SDTProperties.ControlProperties = text;
            rt = new TextRange(document);
            rt.Text = "Text";
            sd.SDTContent.ChildObjects.Add(rt);

            //Add Date Picker Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.DatePicker;
            SdtDate date = new SdtDate();
            date.CalendarType = CalendarType.Default;
            date.DateFormat = "yyyy.MM.dd";
            date.FullDate = DateTime.Now;
            sd.SDTProperties.ControlProperties = date;
            rt = new TextRange(document);
            rt.Text = "1990.02.08";
            sd.SDTContent.ChildObjects.Add(rt);

            //Add Drop-Down List Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.DropDownList;
            SdtDropDownList sddl = new SdtDropDownList();
            sddl.ListItems.Add(new SdtListItem("Harry"));
            sddl.ListItems.Add(new SdtListItem("Jerry"));
            sd.SDTProperties.ControlProperties = sddl;
            rt = new TextRange(document);
            rt.Text = sddl.ListItems[0].DisplayText;
            sd.SDTContent.ChildObjects.Add(rt);

            //Save and launch the file
            string resultfile = "sample.docx";
            document.SaveToFile(resultfile, FileFormat.Docx);
            System.Diagnostics.Process.Start(resultfile);
            
        }
    }
}