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 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 determines how a field is imported. There are three main options for importing fields:
- None
Text fields supported by other applications are not 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. . Instead, form 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. . - MSWord
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. . The ApplicationField 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 allows you to specify which field types to import. - MSWordTXFormFields
Fields 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. , except for form fields. Form fields, including 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. .
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:
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); | |
} | |
} |
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); | |
} |
The resulting PDF document contains the form fields as interactive AcroForms. The following screenshot shows the exported PDF document with the form fields:
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 Form ╰ 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); | |
} | |
} | |
} |
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); | |
} |
The resulting PDF document contains the form fields as flattened text. The following screenshot shows the exported PDF document with the form fields flattened:
Conclusion
TX Text Control provides a powerful API to importing and exporting form fields from MS Word documents. 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 allows you to specify how form fields are imported, and the Form ╰ 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.