When a PDF form is made non-editable, its interactive capability is removed, so the field data appears as regular text and cannot be edited. This is very useful when we want to archive or distribute PDF documents containing forms. In this article, I will demonstrate how to make PDF forms non-editable in Java using Free Spire.PDF for Java.
Add Dependencies
If you are using maven, you can install the jar of Free Spire.PDF for Java into your project by adding the following code to your project’s pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.pdf.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
If you are not using maven, you can download Free Spire.PDF for Java from this website, unzip the package and then import the Spire.Pdf.jar under the lib folder into your project as a dependency.
Making PDF Forms Non-Editable in Java
There are two ways to make PDF forms non-editable, they are: flatten the forms and make the forms read only.
Example 1. Flatten PDF Forms in Java
The following are the steps to flatten all the forms in a PDF document:
- Load a PDF document by initializing an instance of PdfDocument class and passing the file path to the class’s constructor as a parameter.
- Flatten all the forms in the document using PdfDocument.getForm().isFlatten(true) method.
- Save the result document using PdfDocument.saveToFile() method.
Code example:
import com.spire.pdf.PdfDocument;
public class FlattenForms {
public static void main(String[] args){
//Load a PDF document
PdfDocument doc = new PdfDocument("FormFields.pdf");
//Flatten all the form fields in the document
doc.getForm().isFlatten(true);
//Save the result document
doc.saveToFile("Flatten.pdf");
}
}
The input PDF:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/05/inputpdf.png?w=653)
The result PDF:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/05/flatten.png?w=654)
If you only want to flatten a specific form, you can use the following code:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;
public class FlattenSpecificForm {
public static void main(String[] args){
//Load a PDF document
PdfDocument doc = new PdfDocument("FormFields.pdf");
//Access the first form
PdfFormWidget formWidget = (PdfFormWidget)doc.getForm();
PdfField form = (PdfField) formWidget.getFieldsWidget().getList().get(0);
//Flatten the first form
form.setFlatten(true);
//Save the result document
doc.saveToFile("FlattenSpecificForm.pdf");
}
}
Example 2. Make PDF forms Read Only in Java
The following are the steps to make all the forms in a PDF document as read only:
- Load a PDF document by initializing an instance of PdfDocument class and passing the file path to the class’s constructor as a parameter.
- Make all the forms in the document as read only using PdfDocument.getForm().setReadOnly(true) method.
- Save the result document using PdfDocument.saveToFile() method.
Code example:
import com.spire.pdf.PdfDocument;
public class MakeFormsReadOnly {
public static void main(String[] args){
//Load a PDF document
PdfDocument doc = new PdfDocument("FormFields.pdf");
//Make all forms in the document as read only
doc.getForm().setReadOnly(true);
//Save the result document
doc.saveToFile("ReadOnly.pdf");
}
}
The result PDF:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/05/readonly-1.png?w=800)
If you only want to make a specific form read only, you can use the following code:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;
public class MakeSpecificFormReadOnly {
public static void main(String[] args){
//Load a PDF document
PdfDocument doc = new PdfDocument("FormFields.pdf");
//Access the first form
PdfFormWidget formWidget = (PdfFormWidget)doc.getForm();
PdfField form = (PdfField) formWidget.getFieldsWidget().getList().get(0);
//Make the first form read only
form.setReadOnly(true);
//Save the result document
doc.saveToFile("MakeSpecificFormReadOnly.pdf");
}
}