How to Fill PDF Form in Java (Tutorial)

Many people might expect to fill in a PDF field-by-field, but today we will focus instead on filling a PDF programmatically. A use case could arise when the application UI enhances the experience of the user, but it would need to be created with PDF files in an electronic format for archiving.

After collating all data from user input, a librarian must create PDF forms programmatically. The filled-out documents are either saved for future use or modified. There are multiple Java PDF Libraries for PDF work, such as PDFBox, iText7, IronPDF, etc. In this article, we will explain how to use IronPDF to fill interactive forms.

IronPDF a Java Library

IronPDF is a Java PDF library for creating and editing PDFs that allows developers to easily create, edit, and manipulate PDF documents in their Java applications. The library is fully compatible with Java and can be integrated into any Java-based project with just a few lines of code.

IronPDF provides a wide range of features, including support for text and image manipulation, document security, and digital signature capabilities. With IronPDF, developers can quickly and easily generate professional-quality PDF documents, making it a powerful tool for any Java-based project.

Fill PDF Forms using IronPDF

Let's take a look at how we can fill a PDF form programmatically using the Java PDF library.

Install IronPDF in a Maven Project

To install IronPDF Java via Maven, follow these steps:

  1. Open the pom.xml file in your project.
  2. Add the following dependencies within the <dependencies> tag.
  3. Save the pom.xml file and run mvn install in your project directory. This will install IronPDF Java and its dependencies in your project. You can get help from the Sonatype Central Repository Page for IronPDF.

Now you can use IronPDF with your Java code.

Using Java Code to Fill PDF Documents Programmatically

The following code snippet demonstrates how to use the IronPDF library to create and fill PDF forms with Java using HTML markup. The code imports the necessary classes from the IronPDF library and uses the Paths.get method to specify the location where the resulting PDF form will be saved.

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"));
    }
}
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"));
    }
}
JAVA

The first block of code creates a PDF form by converting an HTML form marked up in a string of HTML using the PdfDocument.renderHtmlAsPdf method. The ChromePdfRenderOptions object is used to set the createPdfFormsFromHtml property to true, which makes the form within the HTML markup editable. The resulting PDF is then saved to the specified output location using the saveAs method.

The second block of code demonstrates how to fill in the created PDF form. The PdfDocument.fromFile method is used to load the PDF form from the specified file path. The getForm method is then used to access the form fields, and the setFieldValue method is used to set the values of the firstname and lastname input fields. Finally, the changes are saved to a new file using the saveAs method.

Output

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

Java Fill Pdf Form Tutorial 1 related to Output

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

Java Fill Pdf Form Tutorial 2 related to Output

Summary

In conclusion, IronPDF is a reliable and efficient library for working with PDF documents in Java. Its ability to programmatically fill PDF forms makes it a valuable tool for automating document processing tasks.

IronPDF offers a free trial and affordable licensing options for IronPDF Java starting at $749, making it a cost-effective choice for businesses and developers.

Frequently Asked Questions

What is this Java PDF library?

IronPDF is a Java PDF library for creating and editing PDFs that allows developers to easily create, edit, and manipulate PDF documents in their Java applications.

How can I install this Java library in a Maven project?

To install IronPDF Java via Maven, open the pom.xml file, add the necessary dependencies within the tag, save the file, and run mvn install in your project directory.

How do I fill a PDF form using this library in Java?

You can fill a PDF form by loading an existing PDF with form fields using IronPDF, selecting the form field with getForm method, setting the value with setFieldValue method, and exporting the filled PDF document.

What are the steps to fill a PDF form programmatically with this library?

The steps include installing the Java library, loading the existing PDF, selecting form fields with getForm, setting field values using setFieldValue, and exporting the document.

What features does this library offer for PDF manipulation?

IronPDF offers features like text and image manipulation, document security, and digital signature capabilities, allowing developers to generate professional-quality PDF documents.

Is there a trial version available for this library?

Yes, IronPDF offers a free trial and affordable licensing options for IronPDF Java, making it a cost-effective choice for businesses and developers.

Can this library create PDF forms from HTML?

Yes, IronPDF can create PDF forms from HTML by using the PdfDocument.renderHtmlAsPdf method and setting the createPdfFormsFromHtml property to true.

What is the use of the ChromePdfRenderOptions class in this library?

The ChromePdfRenderOptions class is used to set options for rendering PDFs, such as making forms within HTML markup editable when creating PDFs from HTML.

How do I integrate this library into a Java-based project?

IronPDF can be integrated into any Java-based project by adding the necessary dependencies to the project’s pom.xml file and running the mvn install command to install IronPDF and its dependencies.

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 and problem-solving.

At Iron Software, Darrius enjoys creating new things and simplifying complex concepts to make them more understandable. As one of our resident developers, he has also volunteered to teach students, sharing his expertise with the next generation.

For Darrius, his work is fulfilling because it is valued and has a real impact.