TX Text Control makes it easy to convert Microsoft Word DOCX documents to PDF, including embedded form fields. Whether your DOCX files contain legacy form fields or content controls, TX Text Control ensures that they are accurately preserved in the exported PDF, either as interactive AcroForms or as a flattened, non-editable form.

MS Word Form Fields

There are two types of form fields in MS Word. Legacy form fields, as the name suggests, are from older versions such as MS Word 97, but remain compatible with newer versions. Content Control Form Fields, on the other hand, represent the modern approach to form fields in MS Word. When you load the RTF, DOC, or DOCX (Office Open XML) document types, both can be imported into the TX Text control.

When loading a document, the ApplicationFieldFormat TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
LoadSettings Class
ApplicationFieldFormat Property
Specifies the format of text fields which are imported.
property determines how a field is imported. There are three main options for importing fields:

To load both form fields from MS Word documents, you can use the MSWordTXFormFields setting. The following screenshot shows a sample document with form fields in MS Word:

Form fields in MS Word

Importing Form Fields

The following code demonstrates how to load a document with legacy and content control form fields that are automatically converted to form fields.

using TXTextControl;
TXTextControl.LoadSettings loadSettings = new TXTextControl.LoadSettings()
{
ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWordTXFormFields
};
using (TXTextControl.ServerTextControl serverTextControl =
new TXTextControl.ServerTextControl())
{
serverTextControl.Create();
serverTextControl.Load("forms.docx",
TXTextControl.StreamType.WordprocessingML, loadSettings);
foreach (FormField ff in serverTextControl.FormFields)
{
Console.WriteLine(ff.Text);
}
}
view raw test.cs hosted with ❤ by GitHub

Exporting Form Fields to PDF

Now we can export this document as is to PDF, which will export all the form types it contains to AcroForms. The following code shows how to call the Save TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ServerTextControl Class
Save Method
Saves the complete contents of a document with the specified format.
method after importing the DOCX document.

using TXTextControl;
TXTextControl.LoadSettings loadSettings = new TXTextControl.LoadSettings()
{
ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWordTXFormFields
};
using (TXTextControl.ServerTextControl serverTextControl =
new TXTextControl.ServerTextControl())
{
serverTextControl.Create();
serverTextControl.Load("forms.docx",
TXTextControl.StreamType.WordprocessingML, loadSettings);
serverTextControl.Save("forms.pdf", TXTextControl.StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

The resulting PDF document contains the form fields as interactive AcroForms. The following screenshot shows the exported PDF document with the form fields:

Form fields in PDF

Flattening Form Fields

When a document is finalized for export and sharing as a PDF, form fields are typically flattened. This process converts the form fields into static text, preventing users from making further changes. TX Text Control provides an overload of the FormFieldCollection.Remove TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
FormFieldCollection Class
Remove Method
Removes the specified form field from a Text Control document.
method that does not delete the content and replaces it with plain text. The following extension method flattens all fields in a document.

using TXTextControl;
public static class FormFieldExtender
{
public static void Flatten(this FormFieldCollection formFields, FormField formField)
{
formFields.Remove(formField, true);
}
public static void Flatten(this FormFieldCollection formFields)
{
int fieldCount = formFields.Count;
for (int i = 0; i < fieldCount; i++)
{
TextFieldCollectionBase.TextFieldEnumerator fieldEnum =
formFields.GetEnumerator();
fieldEnum.MoveNext();
FormField curField = (FormField)fieldEnum.Current;
formFields.Remove(curField, true);
}
}
}
view raw test.cs hosted with ❤ by GitHub

The complete code would look like this, along with the loading code above:

using TXTextControl;
TXTextControl.LoadSettings loadSettings = new TXTextControl.LoadSettings()
{
ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWordTXFormFields
};
using (TXTextControl.ServerTextControl serverTextControl =
new TXTextControl.ServerTextControl())
{
serverTextControl.Create();
serverTextControl.Load("forms.docx",
TXTextControl.StreamType.WordprocessingML, loadSettings);
// Flatten all form fields
serverTextControl.FormFields.Flatten();
// Save the document as PDF
serverTextControl.Save("forms.pdf", TXTextControl.StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

The resulting PDF document contains the form fields as flattened text. The following screenshot shows the exported PDF document with the form fields flattened:

Flattened form fields in PDF

Conclusion

TX Text Control provides a powerful API to importing and exporting form fields from MS Word documents. The ApplicationFieldFormat TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
LoadSettings Class
ApplicationFieldFormat Property
Specifies the format of text fields which are imported.
property allows you to specify how form fields are imported, and the FormFieldCollection.Remove TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
FormFieldCollection Class
Remove Method
Removes the specified form field from a Text Control document.
method can be used to flatten form fields before exporting to PDF.