C# – Add Content Controls to Word Documents

Content controls provide a user interface (UI) that has controlled input like a form, preventing users from editing or deleting parts of a document. This is useful if you have information in a document or template that users should be able to read but not edit, or if you want users to be able to edit content controls but not delete them.

There are nine different types of content controls that you can add to documents. This post will introduce how to create most commonly used content controls in Word documents using .NET Word API in C#.

Spire.Doc provides a StructureDocumentTagInline class to work with all types of content controls. First initialize an object of the class, choose the SDTType as DatePicker, then you’re able to create a date picker content control like this:

paragraph = section.AddParagraph();
StructureDocumentTagInline 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;
TextRange rt = new TextRange(document);
rt.Text = "1990.02.08";
sd.SDTContent.ChildObjects.Add(rt);

2016-07-05_152012

For more types, please check below code snippet:

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

namespace ContentControls
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create 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 Picture Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.Picture;
            DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 };
            pic.LoadImage(Image.FromFile(@"D:\Pictures\logo.png"));
            sd.SDTContent.ChildObjects.Add(pic);

            //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);

        }
    }
}
Advertisements