Telerik blogs
DotNetT2 Light_1200x303

Learn how to use the new option for flattening form fields in PdfProcessing.

With R2 2021, the TelerikPdfProcessing library shipped a brand new functionality—the ability to easily remove all fields and preserve their values by calling a single method instead of doing it manually. This new feature allows you to make the document read-only after the user has filled it, and, in this blog post, we will walk you through that functionality.

How to Flatten a Form Field With Telerik PdfProcessing?

To flatten a form field means to merge its current value with the other content on the page. With this action, the fields are converted to text content and cannot be edited anymore. Since the release of the interactive forms (form fields) support in PdfProcessing, many of you asked about flattening these fields. As we are constantly listening to your feedback, we are now introducing that functionality with R2 2021.

In this post, I will show you how to fill the values of a predefined template with interactive forms and how to flatten them.

In the following picture, you will see the example document we are going to fill using PdfProcessing and its API:

PDF document with interactive form fields for first name, last name, phone number, are you a programmer

In this sample document, the form fields are named as follows:

  • firstName
  • lastName
  • phone
  • programmer

To start manipulating the document, first we should import it into a RadFixedDocument instance:

RadFixedDocument document;
using (Stream stream = File.OpenRead(fileName))
PdfFormatProvider provider = new PdfFormatProvider();
document = provider.Import(stream);

This RadFixedDocument instance gives us access to the AcroForm that keeps all the form fields inside the document. The next snippet shows how you can obtain the fields by their names and fill their values:

FormFieldCollection fields = document.AcroForm.FormFields;
TextBoxField firstNameTextBox = fields.FirstOrDefault(f => f.Name == "firstName") as TextBoxField;
firstNameTextBox.Value = "John";
TextBoxField lastNameTextBox = fields.FirstOrDefault(f => f.Name == "lastName") as TextBoxField;
lastNameTextBox.Value = "Doe";
TextBoxField phoneTextBox = fields.FirstOrDefault(f => f.Name == "phone") as TextBoxField;
phoneTextBox.Value = "+1-234-567-8910";
RadioButtonField programmerRadioButton = fields.FirstOrDefault(f => f.Name == "programmer") as RadioButtonField;
RadioOption option = programmerRadioButton.Options.FirstOrDefault(o => o.Value == "Yes");
programmerRadioButton.Value = option;

Now, if you decide to save the document at its current stage, the values will be preserved along with the interactivity of the form fields. The content will look like shown in the next image:

PDF document with filled interactive forms, which could still be clicked into and edited

As you can see, the fields still can be modified. With the new option in PdfProcessing to flatten the forms fields, you can merge the content of the document with the values of the fields just by invoking a single method.

Let’s try the flattening option. The method that flattens the fields values is exposed by the document’s AcroForm:


To see the results, we will need to also save the document that we have already modified:

PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
byte[] documentBytes = pdfFormatProvider.Export(document);
File.WriteAllBytes("InteractiveForms-Flattened.pdf", documentBytes);

And here is how the flattened document looks like:

PDF document after flattening form fields - the fields are no longer separated and have a white background like the rest of the page

All the content inside that document is now simple text and the users cannot further modify it.

That was easy, no?

Try It and Share Your Feedback

Still haven’t tried Telerik Document Processing? Download a free trial and explore all the great features that come with the libraries.

If you are already familiar with the package, don’t forget that we are always happy to hear your feedback. Feel free to drop us a comment below sharing your thoughts. Or visit our Document Processing Libraries Feedback Portal and let us know if you have any suggestions or if you need any particular features.

Happy flattening! 🤘🏼

About the Author

Tanya Dimitrova

Tanya Dimitrova is a Tech Support Engineer in the Telerik XAML Team. In her work her main responsibility is to assist clients to implement different scenarios using the document processing libraries and editors. She is passionate in finding new adventures and in her free time enjoys travelling, reading, swimming, dancing or just spending time with friends.

Related Posts


Comments are disabled in preview mode.