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() method, setting values with setFieldValue(), and saving the filled document - perfect for automating document processing in business applications.

Quickstart: Fill PDF Form in Java

```java {title="FillPDFForm.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")); // 5. Run your Java application to generate the filled PDF


<!-- TODO: Add image here -->
<!-- ![Visual demonstration of introduction](/static-assets/images/TODO/introduction-screenshot.webp) -->
<!-- Description: Screenshot or diagram -->

This article focuses on filling PDF forms programmatically in Java. A common use case is when application UIs collect user data that must be converted to PDF format for archiving.

After collecting user input data, applications need to create PDF forms programmatically. These filled documents are either saved for future use or modified. Multiple Java PDF libraries exist for PDF work, including PDFBox, iText7, and IronPDF. This article explains how to use IronPDF to fill interactive forms. For developers working on business applications that require document automation, IronPDF provides tools for [creating PDF forms](https://ironpdf.com/java/how-to/create-forms/) and managing form data.

<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 Java library to fill PDF form</a></li>
<li>Load existing PDF that contains form fields</li>
<li>Select the form field of the loaded PDF with <code>getForm</code> method</li>
<li>Use <code>setFieldValue</code> method to set the value to specified form field</li>
<li>Export the PDF document</li>
</ol>
</div>

## What Is IronPDF and Why Use It for PDF Forms?

IronPDF is a [Java PDF library for creating and editing PDFs](https://ironpdf.com/java/) that allows developers to create, edit, and manipulate PDF documents in Java applications. The library is compatible with Java and integrates into any Java-based project with minimal code.

IronPDF provides features including text and image manipulation, document security, and digital signature capabilities. With IronPDF, developers can generate PDF documents efficiently. The library also offers support for [PDF compression](https://ironpdf.com/java/how-to/compress-pdf-java-tutorial/) to optimize file sizes and [printing capabilities](https://ironpdf.com/java/how-to/java-print-pdf-tutorial/) for integration with physical printers.

For working with forms, IronPDF maintains document formatting while providing programmatic access to all form elements. This makes it suitable for batch processing forms, pre-populating fields with database information, or integrating form filling into existing Java workflows. The library's API allows you to start filling PDF forms with minimal code, as shown in our [form data examples](https://ironpdf.com/java/examples/form-data/).

## How Do I Fill PDF Forms Using IronPDF?

Let's examine how to fill a PDF form programmatically using this Java PDF library. IronPDF handles various form field types including text boxes, checkboxes, radio buttons, and dropdown lists. The library preserves formatting and ensures filled forms remain compliant with PDF standards.

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

To install IronPDF Java via Maven:

1. Open the `pom.xml` file in your project.  
2. Add the following dependencies within the `<code><dependencies></dependencies></code>` tag.  
3. Save the `pom.xml` file and run `mvn install` in your project directory. This installs IronPDF Java and its dependencies. See the [Sonatype Central Repository Page for IronPDF](https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2023.1.1) for more information.

For detailed setup guidance in different environments, check our [getting started overview](https://ironpdf.com/java/docs/). For cloud deployments, we provide guides for [AWS deployment](https://ironpdf.com/java/get-started/aws/), [Azure Functions](https://ironpdf.com/java/get-started/azure/), and [Google Cloud](https://ironpdf.com/java/get-started/google-cloud/).

Now you can use IronPDF with your Java code.

### How Do I Fill PDF Documents Programmatically with Java?

The following code demonstrates how to use IronPDF to [create and fill PDF forms with Java](https://ironpdf.com/java/examples/form-data/) using HTML markup. The code imports necessary classes from IronPDF and uses the `Paths.get` method to specify where to save the resulting PDF form. This approach is useful when you need to [convert HTML to PDF](https://ironpdf.com/java/tutorials/html-to-pdf/) while maintaining form functionality.

```java
import com.ironsoftware.ironpdf.PdfDocument;  
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;  
import java.io.IOException;  
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class App {

    // Main method to execute the program
    public static void main(String[] args) throws IOException {
        // Specify where the output PDF will be saved
        Path outputLocation = Paths.get("assets/BasicForm.pdf");  

        // HTML string which represents the form to be converted to PDF
        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>";  

        // Set up PDF render options to create forms from HTML
        ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();  
        renderOptions.setCreatePdfFormsFromHtml(true);  

        // Render the HTML as a PDF and save it to the specified location
        PdfDocument.renderHtmlAsPdf(formHTML, renderOptions).saveAs(outputLocation);

        // #2 Use Case: Writing Values to the PDF Form  
        PdfDocument form = PdfDocument.fromFile(outputLocation);  

        // Set the value of the firstname input field  
        form.getForm().setFieldValue("firstname", "Minnie");  

        // Set the value of the lastname input field  
        form.getForm().setFieldValue("lastname", "Mouse");  

        // Save the changes to the PDF Form  
        form.saveAs(Paths.get("assets/BasicForm_Filled.pdf"));
    }
}

The first block creates a PDF form by converting HTML form markup using the PdfDocument.renderHtmlAsPdf method. The ChromePdfRenderOptions object sets the createPdfFormsFromHtml property to true, making the HTML form editable. The resulting PDF is saved to the specified location using the saveAs method. For more rendering options, see our guide on PDF generation settings.

The second block demonstrates filling the created PDF form. The PdfDocument.fromFile method loads the PDF form from the file path. The getForm method accesses form fields, and the setFieldValue method sets values for the firstname and lastname input fields. The changes are saved to a new file using the saveAs method.

What Does the Output Look Like?

In the first block, IronPDF creates a basic PDF document with two text box form fields. The document's PDF specification allows input in the form fields. Here is the output:

Simple PDF form showing editable first name and last name text fields with blue input boxes

For the second block, values are set in each text field, and data fills each form field. Here is the screenshot of the filled PDF document:

Editable PDF form with filled First name 'Minnie' and Last name 'Mouse' fields demonstrating IronPDF form completion

What Are the Key Benefits of Using IronPDF for PDF Forms?

IronPDF offers several advantages for developers working with PDF forms:

  1. Seamless Integration: Integrates with existing Java applications with minimal configuration.

  2. Format Preservation: Maintains exact form formatting during the filling process.

  3. Form Support: Handles text fields, checkboxes, radio buttons, dropdowns, and more.

  4. Batch Processing: Automate filling of multiple forms by iterating through data sets.

  5. Cloud-Ready: Optimized for deployment on cloud platforms for scalable applications.

For secure document handling, IronPDF provides security and metadata capabilities, allowing password protection of filled forms and document property management.

In conclusion, IronPDF is an efficient library for working with PDF documents in Java. Its ability to programmatically fill PDF forms makes it useful for automating document processing tasks. Whether building document management systems, automating invoice generation, or creating personalized reports, IronPDF provides the necessary tools to work with PDF forms.

IronPDF offers a free trial and affordable licensing options for IronPDF Java starting at $799. To get started with IronPDF, visit our API reference for detailed documentation on available features and methods.

Frequently Asked Questions

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

To fill PDF forms in Java using IronPDF, follow these steps: 1) Add IronPDF dependency to your Maven project, 2) Load your existing PDF form using PdfDocument.fromFile(), 3) Access form fields with the getForm() method, 4) Fill fields using setFieldValue() with the field name and value, and 5) Save the filled PDF using saveAs().

How do I access and set values for specific form fields?

IronPDF provides the getForm() method to access form fields in a PDF. Once you have access to the form, use setFieldValue(fieldName, value) to set values for specific fields. For example: form.getForm().setFieldValue("firstname", "John") will fill the firstname field with the value John.

Can I automate form filling for business applications?

Yes, IronPDF is designed for automating document processing in business applications. You can collect user data from application UIs and programmatically fill PDF forms for archiving. The library supports batch processing forms, pre-populating fields with database information, and integrating form filling into existing Java workflows.

What types of PDF documents can be processed?

IronPDF can process existing PDF documents that contain interactive form fields. The library maintains document formatting while providing programmatic access to all form elements including text fields, checkboxes, radio buttons, and other standard PDF form components.

How does the library compare to other Java PDF solutions?

While multiple Java PDF libraries exist including PDFBox and iText7, IronPDF offers a simpler API for form filling with minimal code requirements. It provides comprehensive features for creating, editing, and manipulating PDF documents, along with additional capabilities like PDF compression and printing support.

What additional PDF features are available besides form filling?

Beyond form filling, IronPDF offers text and image manipulation, document security, digital signature capabilities, PDF compression for optimizing file sizes, and printing capabilities for integration with physical printers. The library also supports creating new PDF forms from scratch.

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: 2025.12 just released