Products Technologies Demo Docs Blog Support Company

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.

Extension Method: Flatten Forms Fields in PDF Documents using .NET C#

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 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);
                }
        }
}

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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

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…


ASP.NETASP.NET CoreMarkdown

A Complete Guide to Converting Markdown to PDF in .NET C#

Learn how to convert Markdown to PDF in .NET C# using Text Control's ServerTextControl component. This guide covers setup, conversion process, and customization options for generating high-quality…


ASP.NETASP.NET CoreDocument Creation

Why PDF Creation Belongs at the End of the Business Process

This article discusses why placing PDF creation at the end of the business process is important for ensuring accuracy and efficiency. The most scalable systems delay PDF generation until the…


ASP.NETASP.NET CoreForms

Designing the Perfect PDF Form with TX Text Control in .NET C#

Learn how to create and design interactive PDF forms using TX Text Control in .NET C#. This guide covers essential features and best practices for effective form design.


ASP.NETASP.NET CoreMIME

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…

Summarize this blog post with:

Share on this blog post on: