Extension Method: Flatten Forms Fields in PDF Documents using .NET C#
This article shows how to flatten form fields in TX Text Control before exporting the document to PDF. This is a common requirement when documents should be shared with others and the form fields should not be changed anymore.

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 Form
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);
}
}
}
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);
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();
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);
}
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.
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
ASP.NETASP.NET CoreForm Fields
Create Fillable PDF Forms in .NET C#
This article shows how to create fillable PDF forms in .NET C# using the TX Text Control .NET Server component. The created forms can be filled out using Adobe Reader or any other PDF viewer that…
Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas
Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…
Streamline Data Collection with Embedded Forms in C# .NET
Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…
Adding QR Codes to PDF Documents in C# .NET
This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…
Adding SVG Graphics to PDF Documents in C# .NET
In this article, we will explore how to add SVG graphics to PDF documents using C# .NET. We will use the TX Text Control .NET Server component to demonstrate the process of rendering SVG images in…