How To Add Content Controls To Word Documents In C#

Introduction

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

There are basically ten types of content controls in Word 2013. This article mainly introduces how we can add most commonly used content controls, including Rich Text, Plain Text, Picture, Combo Box, Drop-Down List and Date Picker to Word document at the run time.

Background

I used a .NET Word component to simplify my task, which can be easily downloaded and installed into Visual Studio via NuGet. After installation, we should also add following necessary namespaces at the beginning.

  1. using Spire.Doc;  
  2. using Spire.Doc.Documents;  
  3. using Spire.Doc.Fields;  
  4. using System;  
  5. using System.Drawing;   

Using the code

The following parts will demonstrate how to add a Drop-Down List content control to a Word document. The code snippets of creating other controls is quite similar to this one and can be found at the end of this article.

STEP 1

Create a new Word document, add a section to the document and a paragraph to the section.

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

STEP 2

Initialize an instance of StructureDocumentTagInline class, which represents a content control in Word. Call Paragraph.ChildObjects.Add() method to add the content control to the paragraph and specify its SDT type to Drop-Down List.

  1. StructureDocumentTagInline sd = new StructureDocumentTagInline(document);  
  2. paragraph.ChildObjects.Add(sd);  
  3. sd.SDTProperties.SDTType = SdtType.DropDownList; 

STEP 3

Set the title and tag of the content control.

  1. sdt.SDTProperties.Alias = "Drop-down list";  
  2. sdt.SDTProperties.Tag = "Drop-down list";  

STEP 4

Initialize an instance of SdtDropDownList class, add the items to the list and assign the values to Drop-Down List properties.

  1. SdtDropDownList sddl = new SdtDropDownList();  
  2. sddl.ListItems.Add(new SdtListItem("Male","1"));  
  3. sddl.ListItems.Add(new SdtListItem("Female","2"));  
  4. sdt.SDTProperties.ControlProperties = sddl;  

STEP 5

Set the display text of the control.

  1. TextRange rt = new TextRange(document);          
  2. rt.Text = sddl.ListItems[0].DisplayText;  
  3. sdt.SDTContent.ChildObjects.Add(rt);   

STEP 6

Save the file with the specified file path.

  1. document.SaveToFile("Controls.docx", FileFormat.Docx);   

Entire Code

  1. using Spire.Doc;  
  2. using Spire.Doc.Documents;  
  3. using Spire.Doc.Fields;  
  4. using System;  
  5. using System.Drawing;  
  6.   
  7. namespace ContentControls  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             //Creat a new word document  
  14.             Document document = new Document();  
  15.             Section section = document.AddSection();  
  16.             Paragraph paragraph = section.AddParagraph();  
  17.   
  18.             //Add Drop-Down List Content Control  
  19.             StructureDocumentTagInline sdt = new StructureDocumentTagInline(document);  
  20.             paragraph.ChildObjects.Add(sdt);  
  21.             sdt.SDTProperties.SDTType = SdtType.DropDownList;  
  22.             sdt.SDTProperties.Alias = "Drop-down list";  
  23.             sdt.SDTProperties.Tag = "Drop-down list";  
  24.             SdtDropDownList sddl = new SdtDropDownList();  
  25.             sddl.ListItems.Add(new SdtListItem("Male","1"));  
  26.             sddl.ListItems.Add(new SdtListItem("Female","2"));  
  27.             sdt.SDTProperties.ControlProperties = sddl;  
  28.             TextRange rt = new TextRange(document);          
  29.             rt.Text = sddl.ListItems[0].DisplayText;  
  30.             sdt.SDTContent.ChildObjects.Add(rt);     
  31.   
  32.             //Add Plain Text Content Control  
  33.             paragraph = section.AddParagraph();  
  34.             sdt = new StructureDocumentTagInline(document);  
  35.             paragraph.ChildObjects.Add(sdt);  
  36.             sdt.SDTProperties.SDTType = SdtType.Text;  
  37.             sdt.SDTProperties.Alias = "Plain text";  
  38.             sdt.SDTProperties.Tag = "Plain text";  
  39.             SdtText text = new SdtText(false);  
  40.             text.IsMultiline = true;  
  41.             sdt.SDTProperties.ControlProperties = text;  
  42.             rt = new TextRange(document);  
  43.             rt.Text = "Input only text here.";  
  44.             sdt.SDTContent.ChildObjects.Add(rt);  
  45.   
  46.             //Add Rich Text Content Control  
  47.             paragraph = section.AddParagraph();  
  48.             sdt = new StructureDocumentTagInline(document);  
  49.             paragraph.ChildObjects.Add(sdt);  
  50.             sdt.SDTProperties.SDTType = SdtType.RichText;  
  51.             sdt.SDTProperties.Alias = "Rich text";  
  52.             sdt.SDTProperties.Tag = "Rich text";  
  53.             SdtText richText = new SdtText(true);  
  54.             richText.IsMultiline = true;  
  55.             sdt.SDTProperties.ControlProperties = richText;  
  56.             rt = new TextRange(document);  
  57.             rt.Text = "Insert formatted text tables, pictures, or other content controls here.";  
  58.             sdt.SDTContent.ChildObjects.Add(rt);  
  59.   
  60.             //Add Date Picker Content Control  
  61.             paragraph = section.AddParagraph();  
  62.             sdt = new StructureDocumentTagInline(document);  
  63.             paragraph.ChildObjects.Add(sdt);  
  64.             sdt.SDTProperties.SDTType = SdtType.DatePicker;  
  65.             sdt.SDTProperties.Alias = "Date picker";  
  66.             sdt.SDTProperties.Tag = "Date picker";  
  67.             SdtDate date = new SdtDate();  
  68.             date.CalendarType = CalendarType.Default;  
  69.             date.DateFormat = "yyyy.MM.dd";  
  70.             date.FullDate = DateTime.Now;  
  71.             sdt.SDTProperties.ControlProperties = date;  
  72.             rt = new TextRange(document);  
  73.             rt.Text = "1990.02.08";  
  74.             sdt.SDTContent.ChildObjects.Add(rt);  
  75.   
  76.             //Add Combo Box Content Control  
  77.             paragraph = section.AddParagraph();  
  78.             sdt = new StructureDocumentTagInline(document);  
  79.             paragraph.ChildObjects.Add(sdt);  
  80.             sdt.SDTProperties.SDTType = SdtType.ComboBox;  
  81.             sdt.SDTProperties.Alias = "Combo box";  
  82.             sdt.SDTProperties.Tag = "Combo box";  
  83.             SdtComboBox cb = new SdtComboBox();  
  84.             cb.ListItems.Add(new SdtListItem("U.K.""1"));  
  85.             cb.ListItems.Add(new SdtListItem("Japan""2"));  
  86.             cb.ListItems.Add(new SdtListItem("Italy""3"));  
  87.             sdt.SDTProperties.ControlProperties = cb;  
  88.             rt = new TextRange(document);  
  89.             rt.Text = cb.ListItems[0].DisplayText;  
  90.             sdt.SDTContent.ChildObjects.Add(rt);  
  91.   
  92.             //Add Picture Content Control  
  93.             paragraph = section.AddParagraph();  
  94.             sdt = new StructureDocumentTagInline(document);  
  95.             paragraph.ChildObjects.Add(sdt);  
  96.             sdt.SDTProperties.SDTType = SdtType.Picture;  
  97.             sdt.SDTProperties.Alias = "Picture";  
  98.             sdt.SDTProperties.Tag = "Picture";  
  99.             DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 };  
  100.             pic.LoadImage(Image.FromFile(@"C:\Users\Administrator\Desktop\YourLogo.png"));  
  101.             sdt.SDTContent.ChildObjects.Add(pic);  
  102.   
  103.             //Save the file           
  104.             document.SaveToFile("Controls.docx", FileFormat.Docx);  
  105.         }  
  106.     }  
  107. }  
Ebook Download
View all
Learn
View all