Microsoft Word form fields, such as content controls and legacy form fields, are widely used to create interactive documents for data entry, templates, and workflows. Users can input structured data directly within Word documents with these fields, which are commonly used in contract automation, surveys, and forms.
Building cross-platform .NET applications can be challenging, especially when working with form fields, especially on Linux systems. Fortunately, TX Text Control provides a powerful, consistent API that allows you to import, access, and manipulate both content controls (structured document tags) and legacy form fields in DOCX documents across platforms, including Linux.
What Are MS Word Form Fields?
Microsoft Word supports two main types of form fields:
- Legacy Form Fields:: These are the basic fields used in older versions of Word, such as text inputs, checkboxes, and dropdowns.
- Content Controls:: These were introduced in later versions of Word and offer a more structured, XML-based approach to embedding data in documents.
TX Text Control provides seamless support for both formats through a unified API, making it easy to programmatically create, read, and manipulate form fields in your .NET applications.
How to Work with Form Fields in TX Text Control
When loading a document, the Application ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ LoadSettings Class
╰ ApplicationFieldFormat Property
Specifies the format of text fields which are imported. property can be used for specification of how a field is imported. Three relevant options can be specified when importing fields.
- None: Text fields supported by other applications are not imported as Application
Fields ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
╰ ApplicationFields Property
Gets a collection of all Microsoft Word or Heiler HighEdit fields that have been created or imported from a Microsoft Word or RTF document. . Form fields are imported as TX Text Control FormFields. - MSWord: Fields that are supported and defined by Microsoft Word are imported as ApplicationFields. The Application
Field Type Names ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ LoadSettings Class
╰ ApplicationFieldTypeNames Property
Specifies an array of strings containing the type names of fields which are to be imported. property can be used to select specific types of fields. - MSWordTXFormFields: Fields that are supported and defined by Microsoft Word are imported as ApplicationFields, with the exception of form fields. Form fields such as combo boxes, check boxes, and form text fields are imported as TX Text Control Form
Field ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ FormField Class
The FormField class is the base class of all form fields. .
By using MSWordTXFormFields, all legacy and content control form fields are automatically converted into TX Text Control form fields.
Now, let's take a look at a document that contains both content control form fields and older, legacy form fields.
Accessing Form Fields
Looping through all fields is simple. We just need to load the document with the specific LoadSettings and access the Form ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ FormFieldCollection Class
An instance of the FormFieldCollection class contains all form fields in a Text Control document represented through objects of the types TextFormField, CheckFormField and SelectionFormField. .
using TXTextControl; | |
var loadSettings = new LoadSettings | |
{ | |
ApplicationFieldFormat = ApplicationFieldFormat.MSWordTXFormFields | |
}; | |
using var serverTextControl = new ServerTextControl(); | |
serverTextControl.Create(); | |
serverTextControl.Load("document.docx", StreamType.WordprocessingML, loadSettings); | |
Console.WriteLine("Form Fields in the document:"); | |
foreach (var field in serverTextControl.FormFields) | |
{ | |
switch (field) | |
{ | |
case TextFormField textField: | |
Console.WriteLine($"TextFormField: {textField.Text}"); | |
break; | |
case CheckFormField checkBox: | |
Console.WriteLine($"CheckFormField: {checkBox.Checked}"); | |
break; | |
case DateFormField dateField: | |
Console.WriteLine($"DateFormField: {dateField.Date}"); | |
break; | |
case SelectionFormField selectionField: | |
var selectedItem = selectionField.Items.ElementAtOrDefault(selectionField.SelectedIndex); | |
Console.WriteLine($"SelectionFormField: {selectedItem}"); | |
break; | |
} | |
} |
In this example, we load a DOCX document that contains both content control form fields and legacy form fields. The document is loaded with the MSWordTXFormFields setting, which converts all form fields into TX Text Control form fields.
The following results are printed to the console:
Form Fields in the document:
TextFormField: Content Control Text
CheckFormField: True
SelectionFormField: Item2
DateFormField: 5/8/2025 12:00:00 AM
TextFormField: Text1
CheckFormField: False
Both form field types are accessible in the unified form field provided by the TX Text Control.
Conclusion
TX Text Control provides a powerful API for working with form fields in .NET applications, regardless of the platform. By using the MSWordTXFormFields setting, you can easily convert and access both content control and legacy form fields in your documents.