Flatten Form Fields in PDF in Java

Flattening form fields is an efficient way to prevent other users from editing or deleting the information filled in PDF forms. After flattening, the filled in values will remain visible in the file but the form fields will no longer be editable. This article demonstrates how to flatten all the form fields in a PDF file by using Spire.PDF for Java.

Below is the screenshot of the input PDF file:

Flatten Form Fields in PDF in Java

import com.spire.pdf.PdfDocument;

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

        //Flatten all the form fields in the PDF file
        pdf.getForm().isFlatten(true);

        //Save the result file
        pdf.saveToFile("Flatten.pdf");
        pdf.close();
    }
}

Output:

Flatten Form Fields in PDF in Java