Back to Devexpress

SubDocument.ContentControls Property

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-subdocument-81ff3cbb.md

latest13.7 KB
Original Source

SubDocument.ContentControls Property

Obtains content controls obtained in a document.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
ContentControlCollection ContentControls { get; }
vb
ReadOnly Property ContentControls As ContentControlCollection

Property Value

TypeDescription
ContentControlCollection

A collection of content controls.

|

Remarks

If a document contains content controls, you can print it and export to PDF. Content controls are available in the following formats:

  • DOCX
  • DOTX
  • DOTM
  • DOCM

Create Content Controls

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

Content ControlInterfaceMethod
Building Block GalleryContentControlBuildingBlockGalleryNot available
Check BoxContentControlCheckboxInsertCheckboxControl
Combo BoxContentControlComboBoxInsertComboBoxControl
Date PickerContentControlDateInsertDatePickerControl
Drop-Down ListContentControlDropDownListInsertDropDownListControl
Plain TextContentControlPlainTextInsertPlainTextControl
PictureContentControlPictureNot available
Repeating SectionContentControlRepeatingSectionNot available
Rich TextContentControlRichTextInsertRichTextControl

The code sample below generates a simple appointment form:

csharp
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;
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()
    Dim document As Document = wordProcessor.Document
    Dim contentControls = document.ContentControls

    ' Insert a form to enter a name:
    Dim namePosition = document.CreatePosition(document.Paragraphs(0).Range.End.ToInt() - 1)
    Dim nameControl = contentControls.InsertPlainTextControl(namePosition)

    ' Insert text in a content control:
    Dim 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:
    Dim listPosition = document.CreatePosition(document.Paragraphs(1).Range.End.ToInt() - 1)
    Dim 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:
    Dim datePosition = document.CreatePosition(document.Paragraphs(2).Range.End.ToInt() - 1)
    Dim datePicker = contentControls.InsertDatePickerControl(datePosition)
    datePicker.DateFormat = "dddd, MMMM dd, yyyy"

    ' Insert a checkbox:
    Dim checkboxControl = contentControls.InsertCheckboxControl(document.Paragraphs(3).Range.Start)
    checkboxControl.Checked = False
End Using

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:

csharp
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
    }
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()

  Dim document As Document = wordProcessor.Document
  Dim contentControls = document.ContentControls

  Dim datePickers = document.ContentControls.Where(Function(contentControl) contentControl.ControlType = ContentControlType.Date).Cast(Of ContentControlDate)()

  For Each datePicker In datePickers
    ' your code here
  Next datePicker

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:

csharp
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;
        }
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()

  wordProcessor.LoadDocument("Content Controls.docx")
  Dim document As Document = wordProcessor.Document
  Dim contentControls = document.ContentControls
  Dim firstParagraph = document.Paragraphs(0)
  For i = 0 To contentControls.Count - 1
    If firstParagraph.Range.Contains(contentControls(i).Range.Start) AndAlso contentControls(i).ControlType = ContentControlType.ComboBox Then
      Dim comboBox As ContentControlComboBox = CType(contentControls(i), ContentControlComboBox)

      comboBox.Color = Color.Crimson
      Exit For
    End If
  Next i
End Using

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:

csharp
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);
        }
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()
  Dim document As Document = wordProcessor.Document
  Dim contentControls = document.ContentControls

  Dim i = 0
  Do While i < contentControls.Count
    If contentControls(i).ControlType = ContentControlType.PlainText Then
      contentControls.Remove(contentControls(i), True)
    End If
    i += 1
  Loop
End Using

The following code snippets (auto-collected from DevExpress Examples) contain references to the ContentControls property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-richedit-document-api/CS/DXRichEditControlAPISample/CodeExamples/ContentControlActions.cs#L17

csharp
document.LoadDocument("Documents\\Simple Form.docx");
var contentControls = document.ContentControls;

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/ContentControls.cs#L18

csharp
document.LoadDocument("Documents\\Simple Form.docx");
var contentControls = document.ContentControls;

word-document-api-examples/CS/CodeExamples/ContentControlsActions.cs#L20

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

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/ContentControls.vb#L17

vb
document.LoadDocument("Documents\Simple Form.docx")
Dim contentControls = document.ContentControls
' Insert a form to enter a name:

See Also

SubDocument Interface

SubDocument Members

DevExpress.XtraRichEdit.API.Native Namespace