How to Fill PDF Form in Java

How to Fill PDF Form in Java (Tutorial)

IronPDF enables Java developers to programmatically fill PDF forms by loading existing PDFs, accessing form fields with getForm(), setting values with setFieldValue(), and saving the filled document. This makes it straightforward to automate document processing in business applications.

Quickstart: Fill PDF Form in Java

```java {title="FillPDFForm.java"} //:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/quickstart.java // 1. Add IronPDF dependency to your Maven project // 2. Load your PDF form PdfDocument form = PdfDocument.fromFile("form.pdf"); // 3. Fill form fields form.getForm().setFieldValue("firstname", "John"); // 4. Save the filled PDF form.saveAs(Paths.get("filled_form.pdf"));


<div class="hsg-featured-snippet">
<h2>How to Fill PDF Form in Java</h2>
<ol>
<li><a class="js-modal-open" data-modal-id="download-modal" href="#download-modal">Install the IronPDF Java library via Maven</a></li>
<li>Load the existing PDF that contains form fields using <code>PdfDocument.fromFile()</code></li>
<li>Access the form with the <code>getForm()</code> method</li>
<li>Set field values using <code>setFieldValue(fieldName, value)</code></li>
<li>Save the filled document with <code>saveAs()</code></li>
</ol>
</div>

This article covers how to fill PDF forms programmatically in Java. A common scenario is when an application collects user data through a UI that must be persisted in PDF format for archiving, compliance, or downstream processing.

After user input is captured, applications often need to inject that data directly into pre-existing PDF templates. Multiple Java PDF libraries handle this task, including [Apache PDFBox](https://pdfbox.apache.org), iText, and IronPDF. This guide explains how to use IronPDF to fill interactive forms with text fields, checkboxes, radio buttons, and dropdown lists. For developers building related document workflows, see the guide on [creating PDF forms from scratch in Java](https://ironpdf.com/java/how-to/create-forms/).

## What Is IronPDF for Java?

IronPDF is a [Java PDF library](https://ironpdf.com/java/) for creating, editing, and manipulating PDF documents. It integrates into any Maven-based Java project and exposes a clean API that makes common PDF tasks achievable with minimal boilerplate.

The library covers the full PDF lifecycle: rendering HTML to PDF, reading and writing form data, adding digital signatures, applying security settings, compressing documents, and printing. For working with forms specifically, IronPDF gives programmatic access to every field type in a PDF document while preserving the original formatting. This makes it well-suited for batch form processing, pre-populating templates with database records, or wiring form-filling into a larger Java workflow. See the [IronPDF Java API reference](https://ironpdf.com/java/object-reference/api/) for a full list of available classes and methods.

## How Do I Install IronPDF in a Maven Project?

Add the following dependency block to your `pom.xml` file to install IronPDF for Java.

Open `pom.xml` and insert the dependency inside the `<dependencies>` element, then run `mvn install` to download the library. Available versions are listed on [Maven Central](https://search.maven.org/artifact/com.ironsoftware/ironpdf).

```xml
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/maven-dependency.xml
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>2024.9.1</version>
</dependency>

After installation, import com.ironsoftware.ironpdf.PdfDocument and com.ironsoftware.ironpdf.render.ChromePdfRenderOptions in your Java class to access the form-filling API. For deployment guidance on cloud platforms, see the guides for AWS, Azure, and Google Cloud.

TipsWhen working with IronPDF in a Docker or containerized environment, set the IronPdf.licenseKey system property before the first PdfDocument call to avoid license verification delays at runtime.

How Do I Create and Fill a PDF Form Using HTML in Java?

The most reliable way to produce a consistently structured, fillable PDF is to define the form in HTML and render it with IronPDF. The library converts HTML <input> elements directly into interactive PDF form fields.

The following code creates a two-field PDF form from an HTML string and then fills both fields:

//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/create-and-fill-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import java.io.IOException;
import java.nio.file.Paths;

public class App {

    public static void main(String[] args) throws IOException {

        // Define an HTML form with two text input fields
        String formHTML = "<html>"
            + "<body>"
            + "<h2>Editable PDF Form</h2>"
            + "<form>"
            + "First name: <br><input type='text' name='firstname' value=''><br>"
            + "Last name: <br><input type='text' name='lastname' value=''>"
            + "</form>"
            + "</body>"
            + "</html>";

        // Enable HTML-to-form-field conversion during rendering
        ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
        renderOptions.setCreatePdfFormsFromHtml(true);

        // Render HTML to PDF and save the blank template
        PdfDocument.renderHtmlAsPdf(formHTML, renderOptions)
            .saveAs(Paths.get("assets/BasicForm.pdf"));

        // Load the template and fill in field values
        PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm.pdf"));
        form.getForm().setFieldValue("firstname", "Minnie");
        form.getForm().setFieldValue("lastname", "Mouse");

        // Save the completed form to a new file
        form.saveAs(Paths.get("assets/BasicForm_Filled.pdf"));
    }
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/create-and-fill-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import java.io.IOException;
import java.nio.file.Paths;

public class App {

    public static void main(String[] args) throws IOException {

        // Define an HTML form with two text input fields
        String formHTML = "<html>"
            + "<body>"
            + "<h2>Editable PDF Form</h2>"
            + "<form>"
            + "First name: <br><input type='text' name='firstname' value=''><br>"
            + "Last name: <br><input type='text' name='lastname' value=''>"
            + "</form>"
            + "</body>"
            + "</html>";

        // Enable HTML-to-form-field conversion during rendering
        ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
        renderOptions.setCreatePdfFormsFromHtml(true);

        // Render HTML to PDF and save the blank template
        PdfDocument.renderHtmlAsPdf(formHTML, renderOptions)
            .saveAs(Paths.get("assets/BasicForm.pdf"));

        // Load the template and fill in field values
        PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm.pdf"));
        form.getForm().setFieldValue("firstname", "Minnie");
        form.getForm().setFieldValue("lastname", "Mouse");

        // Save the completed form to a new file
        form.saveAs(Paths.get("assets/BasicForm_Filled.pdf"));
    }
}
JAVA

The first block uses ChromePdfRenderOptions with setCreatePdfFormsFromHtml(true) to tell the rendering engine to treat HTML <input> elements as live PDF form fields rather than static text. The rendered PDF is saved as a reusable template. The second block loads that template, calls getForm().setFieldValue() for each field by name, and writes the result to a separate output file, leaving the original template untouched for future use.

What Does the Output Look Like?

The first rendering produces a PDF with two empty text fields, fully editable in any PDF reader.

Simple PDF form showing editable first name and last name text input fields with blue outlined boxes, rendered by IronPDF from HTML

After setFieldValue() runs, both fields are populated with the supplied data:

IronPDF Java output: PDF form with Minnie in the First name field and Mouse in the Last name field, showing successfully filled form data

How Do I Fill an Existing PDF Form That I Did Not Create?

Many real-world workflows receive third-party PDF templates (government forms, insurance documents, vendor contracts) that already contain named fields. Loading and filling these is the same process: use PdfDocument.fromFile() to load the document, then getForm().setFieldValue() to set each field by its name.

//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-existing-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;

public class FillExistingForm {

    public static void main(String[] args) throws IOException {

        // Load a third-party fillable PDF
        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf"));

        // Set text field values by field name
        form.getForm().setFieldValue("applicant_name", "Jane Smith");
        form.getForm().setFieldValue("date_of_birth", "1985-06-14");
        form.getForm().setFieldValue("reference_number", "REF-2024-00421");

        // Save the completed application
        form.saveAs(Paths.get("output/application_filled.pdf"));
    }
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-existing-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;

public class FillExistingForm {

    public static void main(String[] args) throws IOException {

        // Load a third-party fillable PDF
        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf"));

        // Set text field values by field name
        form.getForm().setFieldValue("applicant_name", "Jane Smith");
        form.getForm().setFieldValue("date_of_birth", "1985-06-14");
        form.getForm().setFieldValue("reference_number", "REF-2024-00421");

        // Save the completed application
        form.saveAs(Paths.get("output/application_filled.pdf"));
    }
}
JAVA

To discover field names in an existing PDF, open the document in Adobe Acrobat Reader, right-click any field, and select "Properties". The field name appears in the General tab. Alternatively, iterate over all fields programmatically:

//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/list-form-fields.java
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.form.FormField;
import java.nio.file.Paths;

public class ListFormFields {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf"));

        // Print the name and type of every form field
        for (FormField field : form.getForm().getFields()) {
            System.out.println("Field: " + field.getName()
                + " | Type: " + field.getType());
        }
    }
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/list-form-fields.java
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.form.FormField;
import java.nio.file.Paths;

public class ListFormFields {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf"));

        // Print the name and type of every form field
        for (FormField field : form.getForm().getFields()) {
            System.out.println("Field: " + field.getName()
                + " | Type: " + field.getType());
        }
    }
}
JAVA

Printing field names before writing data prevents setFieldValue() from silently failing on a mismatched name. Field names in PDF documents are case-sensitive, so "firstname" and "FirstName" are treated as different fields.

Please noteIronPDF's getForm().getFields() returns a list of FormField objects. Each exposes getName(), getType(), and getValue(). These are useful when building dynamic form-fill routines that read field names from an external data source such as a database or CSV file.

How Do I Fill Checkboxes and Radio Buttons in Java?

Text fields accept string values directly, but checkboxes and radio buttons require specific exported values that the PDF defines at authoring time. Use setFieldValue() with the correct export value string for each field.

//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-checkboxes-radio.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;

public class FillCheckboxesAndRadio {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/survey.pdf"));

        // Check a checkbox by setting its exported value (often "Yes" or "On")
        form.getForm().setFieldValue("agree_terms", "Yes");

        // Select a radio button option by its exported value
        form.getForm().setFieldValue("preferred_contact", "email");

        form.saveAs(Paths.get("output/survey_filled.pdf"));
    }
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-checkboxes-radio.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;

public class FillCheckboxesAndRadio {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/survey.pdf"));

        // Check a checkbox by setting its exported value (often "Yes" or "On")
        form.getForm().setFieldValue("agree_terms", "Yes");

        // Select a radio button option by its exported value
        form.getForm().setFieldValue("preferred_contact", "email");

        form.saveAs(Paths.get("output/survey_filled.pdf"));
    }
}
JAVA

The export value for a checkbox is typically "Yes", "On", or a custom string set by the form author. For radio button groups, each option in the group shares the same field name but has a unique export value. Use the field-listing technique from the previous section to confirm the exact export values before writing automated fill routines.

ImportantIf setFieldValue() does not appear to check a checkbox or select a radio button, the export value string is likely wrong. List the field with getFields() and inspect getValue() on a manually checked copy of the PDF to retrieve the correct string.

How Do I Fill Dropdown Lists in a PDF Form Using Java?

Dropdown list fields (also called combo box fields) use the same API as text fields. Pass the option's export value as a string to setFieldValue(). The export value is typically the visible option text unless the form author defined a separate underlying value.

//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-dropdown.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;

public class FillDropdown {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/registration.pdf"));

        // Set a dropdown/combo box field to a specific option
        form.getForm().setFieldValue("country", "United States");

        // Set a list box field (multi-select may require comma-separated values)
        form.getForm().setFieldValue("subscription_tier", "Professional");

        form.saveAs(Paths.get("output/registration_filled.pdf"));
    }
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-dropdown.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;

public class FillDropdown {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("templates/registration.pdf"));

        // Set a dropdown/combo box field to a specific option
        form.getForm().setFieldValue("country", "United States");

        // Set a list box field (multi-select may require comma-separated values)
        form.getForm().setFieldValue("subscription_tier", "Professional");

        form.saveAs(Paths.get("output/registration_filled.pdf"));
    }
}
JAVA

When the dropdown export value differs from the visible label, use getFields() to inspect the field's available options. Passing a value that does not match any option in the field's definition leaves the selection unchanged without throwing an exception, so validating export values before a batch run avoids silent data loss.

TipsTo inspect all available options for a combo box, cast the FormField to FormComboBoxField and call getOptions(). This returns the list of valid export values for that dropdown.

How Do I Flatten a Filled PDF Form in Java?

Flattening converts all form fields into static, non-editable content. This prevents recipients from altering submitted data and is required for many archival and compliance use cases. IronPDF supports form flattening as part of its PDF manipulation capabilities.

//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/flatten-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;

public class FlattenForm {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm_Filled.pdf"));

        // Flatten all form fields -- values become static text
        form.getForm().flatten();

        form.saveAs(Paths.get("output/BasicForm_Archived.pdf"));
    }
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/flatten-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;

public class FlattenForm {

    public static void main(String[] args) throws Exception {

        PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm_Filled.pdf"));

        // Flatten all form fields -- values become static text
        form.getForm().flatten();

        form.saveAs(Paths.get("output/BasicForm_Archived.pdf"));
    }
}
JAVA

After flatten() runs, no interactive form fields remain in the output PDF. The document size typically decreases because the form field overlay layers are removed. Flattened PDFs are safe to distribute without risk of recipients editing the submitted values.

What Are the Next Steps for PDF Form Filling in Java?

IronPDF provides a focused, low-boilerplate API for filling PDF forms in Java. The getForm() method gives direct access to text fields, checkboxes, radio buttons, and dropdown lists. When archival is required, flatten() locks all filled data as static content.

For the next stages of your PDF document workflow, explore these guides:

Start a free trial of IronPDF for Java to test form filling in your own project. When you are ready to deploy, view IronPDF licensing options to find a plan that fits your team's needs.

Frequently Asked Questions

What are the basic steps to fill a PDF form in Java?

Add the IronPDF Maven dependency, load your PDF with PdfDocument.fromFile(), access form fields with getForm(), call setFieldValue(fieldName, value) for each field, and save the result with saveAs().

How do I discover field names in an existing PDF?

Open the PDF in Adobe Acrobat Reader, right-click any field, and select Properties. The field name appears in the General tab. Alternatively, call getForm().getFields() to iterate all fields and print each getName() and getType() programmatically.

How do I fill checkboxes and radio buttons in Java?

Use setFieldValue() with the field's export value string. Checkbox export values are typically Yes or On. For radio button groups, each option shares the same field name but has a unique export value. If the field does not respond, inspect getValue() on a manually checked copy of the PDF to find the correct string.

How do I fill a dropdown list field?

Call setFieldValue(fieldName, optionValue) with the option's export value. The export value is typically the visible option text. To list available options programmatically, cast the FormField to FormComboBoxField and call getOptions().

What does flattening a PDF form do?

Flattening converts all interactive form fields into static, non-editable content. Call getForm().flatten() before saving. The resulting PDF cannot be edited by recipients, which is required for many archival and compliance workflows.

Can IronPDF fill forms created by other tools?

Yes. PdfDocument.fromFile() loads any standard fillable PDF regardless of how it was created. Use getForm().getFields() to inspect field names before calling setFieldValue(), since field names are case-sensitive and vary by form author.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity ...

Read More
Ready to Get Started?
Version: 2026.4 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast?
run a sample watch your HTML become a PDF.