Content controls are individual controls that you can add and customize for use in templates, forms, and documents. These controls are commonly used in MS Word templates and forms and replace the old form fields. Both types of form fields are supported by TX Text Control and will be automatically imported and converted to TX Text Control form fields.
Important
Only the Office Open XML (DOCX) format, not the MS Word 97-2003 document format, supports content controls in MS Word documents.
If you are using fields from other applications, such as MS Word, there are a number of ways to handle these fields. When loading a document, the Application
╰ TXTextControl Namespace
╰ LoadSettings Class
╰ ApplicationFieldFormat Property
Specifies the format of text fields which are imported. property can be used to specify how a field is imported. There are three relevant options that can be specified when importing fields:
- None
Text fields supported by other applications are not imported as ApplicationFields. Form fields are imported as TX Text Control FormFields. - MSWord
Fields that are supported and defined by Microsoft Word are imported as ApplicationFields. The ApplicationField ╰ TX Text Control .NET Server for ASP.NETType Names
╰ 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 ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ApplicationField Class
The ApplicationField class supports text field formats of applications such as Microsoft Word. , with the exception of form fields. Form fields such as combo boxes, check boxes, and form text fields are imported as TX Text Control FormFields ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ FormField Class
The FormField class is the base class of all form fields. .
Field Overview
The table below shows how fields are imported based on the specified LoadSettings:
Field | Document Format | Imported as ( Load ╰ TXTextControl Namespace ╰ LoadSettings Class ╰ ApplicationFieldFormat Property Specifies the format of text fields which are imported. ) |
||
None | MSWord | MSWordTXFormFields | ||
TextField | InternalUnicodeFormat, InternalFormat | Text ╰ TXTextControl Namespace ╰ TextField Class An instance of the TextField class represents a text field in a Text Control document. |
N/A | N/A |
MergeField | MSWord, WordprocessingML, RichTextFormat | × | Application ╰ TXTextControl Namespace ╰ ApplicationField Class The ApplicationField class supports text field formats of applications such as Microsoft Word. |
Application ╰ TXTextControl Namespace ╰ ApplicationField Class The ApplicationField class supports text field formats of applications such as Microsoft Word. |
DOCVARIABLE | MSWord, WordprocessingML, RichTextFormat | × | Application ╰ TXTextControl Namespace ╰ ApplicationField Class The ApplicationField class supports text field formats of applications such as Microsoft Word. |
Application ╰ TXTextControl Namespace ╰ ApplicationField Class The ApplicationField class supports text field formats of applications such as Microsoft Word. |
Content Control | MSWord, WordprocessingML, RichTextFormat | Form ╰ TXTextControl Namespace ╰ FormField Class The FormField class is the base class of all form fields. |
Application ╰ TXTextControl Namespace ╰ ApplicationField Class The ApplicationField class supports text field formats of applications such as Microsoft Word. |
Form ╰ TXTextControl Namespace ╰ FormField Class The FormField class is the base class of all form fields. |
Legacy Form Fields | MSWord, WordprocessingML, RichTextFormat | Form ╰ TXTextControl Namespace ╰ FormField Class The FormField class is the base class of all form fields. |
Application ╰ TXTextControl Namespace ╰ ApplicationField Class The ApplicationField class supports text field formats of applications such as Microsoft Word. |
Form ╰ TXTextControl Namespace ╰ FormField Class The FormField class is the base class of all form fields. |
Importing Content Controls
Consider the following MS Word document to which several Content Controls have been added.
The following code shows how to load the document with Content Control fields automatically converted to TX Text Control fields.
TXTextControl.LoadSettings loadSettings = new TXTextControl.LoadSettings() | |
{ | |
ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWordTXFormFields | |
}; | |
using (TXTextControl.ServerTextControl serverTextControl = | |
new TXTextControl.ServerTextControl()) | |
{ | |
serverTextControl.Create(); | |
serverTextControl.Load("contentcontrols.docx", | |
TXTextControl.StreamType.WordprocessingML, loadSettings); | |
foreach (FormField ff in serverTextControl.FormFields) | |
{ | |
Console.WriteLine(ff.Text); | |
} | |
} |
When the document is loaded into a visual TX Text Control, the Content Control fields are rendered as form fields by default.
Processing Content Controls
Another way to access Content Controls is to import the document without converting them to form fields. In this case, Content Controls are accessible through the Application
╰ 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. collection to provide more access to the contained data.
The DocumentServer includes the Fields ╰ TX Text Control .NET Server for ASP.NET
╰ DocumentServer.Fields Namespace
TXTextControl.DocumentServer.Fields Namespace namespace, which contains field classes for retrieving additional data from Content Controls such as Title ╰ TX Text Control .NET Server for ASP.NET
╰ DocumentServer.Fields Namespace
╰ ContentControlFieldAdapter Class
╰ Title Property
Gets or sets the content control's title. or Tag ╰ TX Text Control .NET Server for ASP.NET
╰ DocumentServer.Fields Namespace
╰ ContentControlFieldAdapter Class
╰ Tag Property
Gets or sets the content control's tag. .
The following code shows how to access the Content Controls in the document.
LoadSettings ls = new LoadSettings() | |
{ | |
ApplicationFieldFormat = ApplicationFieldFormat.MSWord | |
}; | |
using (ServerTextControl tx = new ServerTextControl()) | |
{ | |
tx.Create(); | |
tx.Load("contentcontrols.docx", StreamType.WordprocessingML, ls); | |
foreach (ApplicationField af in tx.ApplicationFields) | |
{ | |
try | |
{ | |
var tempField = ContentControlFieldAdapter.CreateContentControl(af); | |
if (tempField is RichTextContentControl || | |
tempField is PlainTextContentControl || | |
tempField is CheckBoxContentControl || | |
tempField is ComboBoxContentControl || | |
tempField is DateContentControl || | |
tempField is DropDownListContentControl) | |
{ | |
Console.WriteLine(tempField.Title); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
} |
Conclusion
Content controls are used to create structured documents and forms. This article showed how to import and process content controls in MS Word documents using TX Text Control. TX Text Control supports legacy form fields, merge fields, and Content Control fields, making them easy to import and edit.