TX Text Control provides extensive functionality for creating form documents with form fields such as text boxes, checkboxes, or drop-down lists. These documents can be deployed using TX Text Control UI components that help users fill out these forms, or you can programmatically create fillable PDFs.

When a document is completed and you want to export and share the completed document as a PDF, form fields are typically flattened. This means that the form fields are converted to static text and the user can't change the content of the form fields anymore.

Flatten Form Fields

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.

In order to make it very easy to use, in this article we have implemented two extension methods for the FormFieldCollection.

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 first implementation removes a specific form field from the collection and replaces it with plain text. The second implementation removes all form fields from the collection and replaces them with plain text.

These extension methods can be used in the following way:

FormField curField = textControl1.FormFields.GetItem(3);
textControl1.FormFields.Flatten(curField);
view raw test.cs hosted with ❤ by GitHub

The above code snippet removes one specific form field from the collection and replaces it with plain text.

When you want to remove all form fields from the collection and replace them with plain text, you can use the following code snippet:

textControl1.FormFields.Flatten();
view raw test.cs hosted with ❤ by GitHub

Typically, you would flatten the form fields before exporting the document to PDF. The following code snippet shows how to flatten all form fields in a document and to export the document to PDF:

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
// Load a document with form fields
tx.Load("form1.tx", StreamType.InternalUnicodeFormat);
// Flatten all form fields
tx.FormFields.Flatten();
// Save the document as PDF
tx.Save("form1.pdf", StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

Conclusion

Flattening form fields in a document is a common requirement when exporting documents to PDF. The extension methods provided in this article help to flatten form fields in a document and replace them with plain text.

These methods can be used to remove specific form fields or all form fields from a document. The flattened document can be exported to PDF and shared with others.