Skip to main content
All docs
V24.1

Content Controls in Word Processing Documents

  • 5 minutes to read

Content controls are interactive elements (text fields, drop-down lists, date pickers) that allow users to input and manage information. Content controls are often used in templates or forms to standardize document formatting and streamline data entry.

content controls

Word Processing Document API allows you to manage content controls in code. If a document contains such controls, you can print it and export to PDF. Content controls are available in the following formats:

  • DOCX
  • DOTX
  • DOTM
  • DOCM

Note

Content controls are not converted to form fields when you export a document to the PDF format.

Create Content Controls

The table below lists supported content control types and API used to create each type (if available).

Content Control Interface Method
Building Block Gallery ContentControlBuildingBlockGallery Not available
Check Box ContentControlCheckbox InsertCheckboxControl
Combo Box ContentControlComboBox InsertComboBoxControl
Date Picker ContentControlDate InsertDatePickerControl
Drop-Down List ContentControlDropDownList InsertDropDownListControl
Plain Text ContentControlPlainText InsertPlainTextControl
Picture ContentControlPicture Not available
Repeating Section ContentControlRepeatingSection Not available
Rich Text ContentControlRichText InsertRichTextControl

The code sample below generates a simple appointment form:

content controls simple forms

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {
    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;

    // Insert a form to enter a name:
    var namePosition = document.CreatePosition(document.Paragraphs[0].Range.End.ToInt() - 1);    
    var nameControl = contentControls.InsertPlainTextControl(namePosition);

    // Insert text in a content control:
    var nameTextPosition = document.CreatePosition(nameControl.Range.Start.ToInt() + 1);
    document.InsertText(nameTextPosition, "Click to enter a name");

    // Insert a drop-down list to select the appointment type:
    var listPosition = document.CreatePosition(document.Paragraphs[1].Range.End.ToInt() - 1);
    var listControl = contentControls.InsertDropDownListControl(listPosition);

    // Add items to the drop-down list:
    listControl.AddItem("First Appointment", "First Appointment");
    listControl.AddItem("Follow-Up Appointment", "Follow-Up Appointment");
    listControl.AddItem("Laboratory Results Check", "Laboratory Results Check");

    listControl.SelectedItemIndex = 1;

    // Insert a date picker to select the appointment date:
    var datePosition = document.CreatePosition(document.Paragraphs[2].Range.End.ToInt() - 1);
    var datePicker = contentControls.InsertDatePickerControl(datePosition);
    datePicker.DateFormat = "dddd, MMMM dd, yyyy";

    // Insert a checkbox:
    var checkboxControl = contentControls.InsertCheckboxControl(document.Paragraphs[3].Range.Start);
    checkboxControl.Checked = false;
}

Access Content Controls

The SubDocument.ContentControls property obtains all content controls in a document. Use the ContentControlBase.ControlType property to determine the content control type.

The code sample below shows how to obtain all date pickers in a document:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {

    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;

    var datePickers = document.ContentControls.Where(contentControl => contentControl.ControlType == ContentControlType.Date).Cast<ContentControlDate>();

    foreach (ContentControlDate datePicker in datePickers)
    {
        // your code here
    }

Modify Content Controls

You can use the content control’s class members to change its parameters.

The code sample below retrieves the combo box from the first paragraph and changes its border color:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {

    wordProcessor.LoadDocument("Content Controls.docx");
    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;
    var firstParagraph = document.Paragraphs[0];
    for (var i = 0; i < contentControls.Count; i++)
    {
        if (firstParagraph.Range.Contains(contentControls[i].Range.Start) && contentControls[i].ControlType == ContentControlType.ComboBox)
        {
            ContentControlComboBox comboBox = (ContentControlComboBox)contentControls[i];

            comboBox.Color = Color.Crimson;
            break;
        }
    }
}

Remove Content Controls

The ContentControlCollection.Remove method allows you to remove a specific content control. You can also specify whether to keep control’s contents when the control is removed.

The code sample below removes all plain text content controls from a document:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {
    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;

    for (var i = 0; i < contentControls.Count; i++)
    {
        if (contentControls[i].ControlType == ContentControlType.PlainText)
        {
            contentControls.Remove(contentControls[i], true);
        }
    }
}