How to Flatten PDF Form Fields in Java

This quick tutorial provides details on how to flatten PDF form fields in Java. You may require this feature where form fields are filled already and you just want to show these filled fields preventing them to be updated. At the end of this article, you will be able to load and flatten PDF form in Java and save it again as a PDF file.

Steps to Flatten PDF Form Fields in Java

  1. Install Aspose.PDF into your application to flatten PDF in Java
  2. Instantiate the Acro Form class object for flattening Adobe PDF
  3. Bind source PDF file with the Form object by calling bindPdf function
  4. Flatten form fields using the Form object after binding the source PDF file
  5. Save the flattened PDF file on disc

This step-by-step tutorial guides you to flatten fillable PDF in Java by creating an Acro Form class object and binding it with the source PDF file. After binding, you may call the flattenAllFields() function of Form class to flatten PDF file and finally save as flattened PDF in Java.

Code to Flatten Fillable PDF in Java

In this code, we initialize the Form class object without any argument however you can initialize it using a PDF Document object or the source file name. It is not necessary to flatten all the fields but you can flatten individual fields also by calling FlattenField() function with field name instead of calling the flattenAllFields() function.

In this tutorial, you learned to flatten PDF file in Java however, if you are looking for steps to fetch text and images in a PDF file, refer to the article on how to read PDF file in Java.

 English