Products Technologies Demo Docs Blog Support Company

Working with Content Controls in MS Word Documents using TX Text Control in C#

This article shows how to work with content controls in MS Word documents using TX Text Control in C#. Content controls are used to create structured documents and forms. This article shows how to import and process content controls in MS Word documents using TX Text Control.

Working with Content Controls in MS Word Documents using TX Text Control in C#

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 ApplicationFieldFormat 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 ApplicationFieldTypeNames 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 FormFields.

Field Overview

The table below shows how fields are imported based on the specified LoadSettings:

Field Document Format Imported as ( LoadSettings)
None MSWord MSWordTXFormFields
TextField InternalUnicodeFormat, InternalFormat TextField N/A N/A
MergeField MSWord, WordprocessingML, RichTextFormat × ApplicationField ApplicationField
DOCVARIABLE MSWord, WordprocessingML, RichTextFormat × ApplicationField ApplicationField
Content Control MSWord, WordprocessingML, RichTextFormat FormFields ApplicationField FormFields
Legacy Form Fields MSWord, WordprocessingML, RichTextFormat FormFields ApplicationField FormFields

Importing Content Controls

Consider the following MS Word document to which several Content Controls have been added.

Content Controls in TX Text Control

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.

Content Controls in TX Text Control

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 ApplicationFields collection to provide more access to the contained data.

The DocumentServer includes the Fields namespace, which contains field classes for retrieving additional data from Content Controls such as Title or 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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETASP.NET CoreDOCX

How to Import and Read Form Fields from DOCX Documents in .NET on Linux

Learn how to import and read form fields from DOCX documents in .NET on Linux using TX Text Control. This article provides a step-by-step guide to help you get started with form fields in TX Text…


ASP.NETASP.NET CoreDOCX

How to Programmatically Create MS Word DOCX Documents with .NET C# on Linux

Learn how to programmatically create MS Word DOCX documents using .NET C# on Linux with TX Text Control. This tutorial covers the necessary steps to set up your environment and create a simple…


ASP.NETASP.NET CoreDOCX

Edit MS Word DOCX Files in .NET C# and ASP.NET Core

This article shows how to edit MS Word DOCX files in .NET C# and ASP.NET Core using the TX Text Control Document Editor and also how to manipulate documents without a user interface using the…


ASP.NETASP.NET CoreDOC

Create Word Document with .NET C#

This article provides a guide on using TX Text Control to create MS Word DOCX and DOC documents in .NET applications, such as ASP.NET Core and Windows applications. It covers steps to set up a…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…