Flatten That PDF With Telerik Document Processing

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


This content originally appeared on Telerik Blogs and was authored by Tanya Dimitrova

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:

document.AcroForm.FlattenFormFields();

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!


This content originally appeared on Telerik Blogs and was authored by Tanya Dimitrova


Print Share Comment Cite Upload Translate Updates
APA

Tanya Dimitrova | Sciencx (2021-07-19T10:46:00+00:00) Flatten That PDF With Telerik Document Processing. Retrieved from https://www.scien.cx/2021/07/19/flatten-that-pdf-with-telerik-document-processing/

MLA
" » Flatten That PDF With Telerik Document Processing." Tanya Dimitrova | Sciencx - Monday July 19, 2021, https://www.scien.cx/2021/07/19/flatten-that-pdf-with-telerik-document-processing/
HARVARD
Tanya Dimitrova | Sciencx Monday July 19, 2021 » Flatten That PDF With Telerik Document Processing., viewed ,<https://www.scien.cx/2021/07/19/flatten-that-pdf-with-telerik-document-processing/>
VANCOUVER
Tanya Dimitrova | Sciencx - » Flatten That PDF With Telerik Document Processing. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/19/flatten-that-pdf-with-telerik-document-processing/
CHICAGO
" » Flatten That PDF With Telerik Document Processing." Tanya Dimitrova | Sciencx - Accessed . https://www.scien.cx/2021/07/19/flatten-that-pdf-with-telerik-document-processing/
IEEE
" » Flatten That PDF With Telerik Document Processing." Tanya Dimitrova | Sciencx [Online]. Available: https://www.scien.cx/2021/07/19/flatten-that-pdf-with-telerik-document-processing/. [Accessed: ]
rf:citation
» Flatten That PDF With Telerik Document Processing | Tanya Dimitrova | Sciencx | https://www.scien.cx/2021/07/19/flatten-that-pdf-with-telerik-document-processing/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.