Back to Devexpress

How to: Create a Check Box Form Field

officefileapi-120712-word-processing-document-api-examples-document-elements-how-to-insert-a-checkbox.md

latest5.2 KB
Original Source

How to: Create a Check Box Form Field

  • Nov 30, 2023
  • 3 minutes to read

The RichEditDocumentServer allows you to view and export a document containing checkbox form fields. These form fields are interactive, so users can toggle their state as needed. The resulting document can be exported to PDF or HTML format.

Insert a Checkbox

Use members from the table below to insert a checkbox in code.

APIDescription
FormFieldCollection.InsertCheckBoxInserts a checkbox form field to a given document position and adds the created CheckBox object to the FormFieldCollection. Note that you can only insert checkboxes in the main document body. An FormFieldIncorrectSubDocumentException occurs on attempt to insert the checkbox into a comment, header, footer or text box.
FormField.NameSpecifies a bookmark name associated with the checkbox.
CheckBox.StateGets or sets the checkbox’s state.
CheckBox.SizeModeDefines the checkbox’s size mode. When you set this property to CheckBoxSizeMode.Auto the check box is resized according to the current font size value (returned by the CharacterPropertiesBase.FontSize property). Use the CheckBox.Size property to specify the exact size.
CheckBox.SizeSpecifies the exact size of the check box. Set the CheckBox.SizeMode to CheckBoxSizeMode.Exact to use this property.
FormField.HelpTextType
FormField.StatusTextTypeSpecifies the instructional text type that accompanies the checkbox. This text can be displayed in the status bar or when focusing the checkbox and pressing F1.
FormField.HelpText
FormField.StatusTextGets or sets the checkbox’s instructional text. With the …TextType property set to FormFieldTextType.Auto, make sure that the …Text property value is equal to one of the document’s AutoText gallery entries (stored as the Normal.dotm file in the system’s Templates folder). Otherwise, the help text is not displayed.
FormField.CalculateOnExitSpecifies whether to update the checkbox’s value on exit.

The code sample below shows how to insert a checkbox:

View Example

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

using (var wordProcessor = new RichEditDocumentServer()) {
    DocumentPosition currentPosition = wordProcessor.Document.Range.Start;
    CheckBox checkBox = wordProcessor.Document.FormFields.InsertCheckBox(currentPosition);
    checkBox.Name = "check1";
    checkBox.State = CheckBoxState.Checked;
    checkBox.SizeMode = CheckBoxSizeMode.Auto;
    checkBox.HelpTextType = FormFieldTextType.Custom;
    checkBox.HelpText = "help text";
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()
    Dim currentPosition As DocumentPosition = wordProcessor.Document.Range.Start
    Dim checkBox As CheckBox = wordProcessor.Document.FormFields.InsertCheckBox(currentPosition)
    checkBox.Name = "check1"
    checkBox.State = CheckBoxState.Checked
    checkBox.SizeMode = CheckBoxSizeMode.Auto
    checkBox.HelpTextType = FormFieldTextType.Custom
    checkBox.HelpText = "help text"
End Using

Limitations

The current check box form field’s implementation has the following limitations:

  • The other form field and content control types are not supported - they cannot be imported in our document model and preserved on document save.
  • RichEditDocumentServer does not support checkboxes in OpenDocument Text (.odt) and HTML documents.