Java: Flatten Form Fields in PDF

Form fields are often used in PDF documents to collect information from users. In some cases, you may need to flatten form fields in a PDF. For instance, when you want to prevent other viewers from editing the information you entered in the form fields of a PDF questionnaire. This article will introduce how to flatten form fields in PDF in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you need to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.4.4</version>
    </dependency>
</dependencies>
    

Flatten a Specific Form Field in PDF in Java

The following are the steps to flatten a specific form field in a PDF document using Spire.PDF for Java:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the form widget collection from the document.
  • Get a specific form field from the widget collection by its name or index through PdfFormWidget.getFieldsWidget().get() method.
  • Flatten the form field through PdfField.setFlatten() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;

public class FlattenSpecificFormField {
    public static void main(String[] args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF document
        pdf.loadFromFile("Form.pdf");

        //Get the form widget collection
        PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm();
        //Get a specific form field by its name
        PdfField form = formWidget.getFieldsWidget().get("Address");
        //Get a specific form field by its index
        //PdfField form = formWidget.getFieldsWidget().get(2);
        //Flatten the form
        form.setFlatten(true);

        //Save the result document
        pdf.saveToFile("FlattenSpecific.pdf");
    }
}

Java:  Flatten Form Fields in PDF

Flatten All Form Fields in PDF in Java

The following are the steps to flatten all the form fields in a PDF document using Spire.PDF for Java:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Flatten all the form fields in the document through PdfDocument.getForm().isFlatten() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;

public class FlattenAllFormFields {
    public static void main(String[] args){        
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF document
        pdf.loadFromFile("Form.pdf");

        //Flatten all the forms in the document
        pdf.getForm().isFlatten(true);

        //Save the result document
        pdf.saveToFile("FlattenAll.pdf");
    }
}

Java:  Flatten Form Fields in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.