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 PDF Box, 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.*;

public class App {
    public static main(String [] args) throws IOException {
        Path outputLocation = Paths.get("assets/BasicForm.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>";  

        ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();  
        renderOptions.setCreatePdfFormsFromHtml(true);  
        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.