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…
Why Defining MIME Types for PDF/A Attachments Is Essential
The PDF/A standard was created to ensure the long-term reliable archiving of digital documents. An important aspect of the standard involves properly handling embedded files and attachments within…
Validate Digital Signatures and the Integrity of PDF Documents in C# .NET
Learn how to validate digital signatures and the integrity of PDF documents using the PDF Validation component from TX Text Control in C# .NET. Ensure the authenticity and compliance of your…
Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET
The new TXTextControl.PDF.Validation NuGet package enables you to validate PDF/UA documents and verify digital signatures directly in your code without relying on third-party tools or external…
How To Choose the Right C# PDF Generation Library: Developer Checklist
To make your choice easy, this guide provides a systematic evaluation framework for two library categories: basic and enterprise PDF libraries. It covers matching features to use cases, evaluating…
