How to Create PDF Forms in Java

If your business spends too much on annual PDF form creation and customization tools, IronPDF for Java provides a powerful solution. With it, you can create dynamic, interactive PDF forms that accept user input, enable selection, and even save changes. Whether you're creating text inputs, checkboxes, or more advanced form fields, this guide will show you how to get started.

Create Forms

IronPDF lets you create PDF forms from HTML, meaning you can leverage the full power of HTML, CSS, and JavaScript. This flexibility allows you to embed form fields and other elements into PDFs easily. Let’s dive into how you can implement these features with Java.

Text Input and TextArea Forms

Using IronPDF, you can quickly create input and textarea elements within your PDF by rendering an HTML string. Since it supports HTML, you can apply CSS for styling and, depending on your environment, potentially use JavaScript for additional behavior.

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Define the HTML content with form fields
String htmlContent = """
<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=''> <br>
            Address: <br> <textarea name='address' rows='4' cols='50'></textarea>
        </form>
    </body>
</html>
""";

// Generate a PDF document from the HTML content
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);

// Save the generated PDF to a file
pdfDoc.saveAs("textAreaAndInputForm.pdf");
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Define the HTML content with form fields
String htmlContent = """
<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=''> <br>
            Address: <br> <textarea name='address' rows='4' cols='50'></textarea>
        </form>
    </body>
</html>
""";

// Generate a PDF document from the HTML content
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);

// Save the generated PDF to a file
pdfDoc.saveAs("textAreaAndInputForm.pdf");
JAVA

With the text form field HTML, use the renderHtmlAsPdf method of the PdfDocument class to convert it into a PDF document. The resulting PDF is saved to a file. This example demonstrates how to embed and customize HTML forms within a PDF document using IronPDF, allowing for rich, interactive forms directly in the PDF format.

Output PDF Document:


Checkbox and Combobox Forms

Checkbox and combobox forms can also be generated by rendering an HTML string, file, or web URL that includes these elements. To enable their creation, set the CreatePdfFormsFromHtml property to true.

Combobox forms allow users to select from a dropdown menu of options, providing a convenient way to capture input directly within the PDF document.

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Define the HTML content with form fields
String htmlContent = """
<html>
    <body>
        <h2>Editable PDF Form</h2>
        <h2>Task Completed</h2>
        <label>
            <input type='checkbox' id='taskCompleted' name='taskCompleted'> Mark task as completed
        </label>
        <h2>Select Priority</h2>
        <label for='priority'>Choose priority level:</label>
        <select id='priority' name='priority'>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
        </select>
    </body>
</html>
""";

// Generate a PDF document from the HTML content
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);

// Save the generated PDF to a file
pdfDoc.saveAs("checkboxAndComboboxForm.pdf");
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Define the HTML content with form fields
String htmlContent = """
<html>
    <body>
        <h2>Editable PDF Form</h2>
        <h2>Task Completed</h2>
        <label>
            <input type='checkbox' id='taskCompleted' name='taskCompleted'> Mark task as completed
        </label>
        <h2>Select Priority</h2>
        <label for='priority'>Choose priority level:</label>
        <select id='priority' name='priority'>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
        </select>
    </body>
</html>
""";

// Generate a PDF document from the HTML content
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);

// Save the generated PDF to a file
pdfDoc.saveAs("checkboxAndComboboxForm.pdf");
JAVA

Output PDF Document:


Radio Button Forms

In IronPDF, radio buttons within the same group are part of a single form object. You can access all form fields using the getForm method followed by the getFields method. If a radio button is selected, the form's Value property will reflect that choice; if none are selected, the value will be set to 'None'.

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Define the HTML content with radio button form fields
String htmlContent = """
<html>
    <body>
        <h2>Editable PDF Form</h2>
        Choose your preferred travel type: <br>
        <input type='radio' name='traveltype' value='Bike'> Bike <br>
        <input type='radio' name='traveltype' value='Car'> Car <br>
        <input type='radio' name='traveltype' value='Airplane'> Airplane
    </body>
</html>
""";

// Generate a PDF document from the HTML content
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);

// Save the generated PDF to a file
pdfDoc.saveAs("radioButtonForm.pdf");
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

// Define the HTML content with radio button form fields
String htmlContent = """
<html>
    <body>
        <h2>Editable PDF Form</h2>
        Choose your preferred travel type: <br>
        <input type='radio' name='traveltype' value='Bike'> Bike <br>
        <input type='radio' name='traveltype' value='Car'> Car <br>
        <input type='radio' name='traveltype' value='Airplane'> Airplane
    </body>
</html>
""";

// Generate a PDF document from the HTML content
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);

// Save the generated PDF to a file
pdfDoc.saveAs("radioButtonForm.pdf");
JAVA

Frequently Asked Questions

How can I create PDF forms in Java without losing formatting?

To create PDF forms in Java without losing formatting, you can use IronPDF. Start by installing the IronPDF library, create an HTML string with the desired form fields, and utilize the renderHtmlAsPdf method to convert this HTML to a PDF document.

What types of form fields can be added to PDFs using this library?

IronPDF supports a variety of form fields including text inputs, text areas, checkboxes, comboboxes, and radio buttons, all of which can be embedded into a PDF through rendering an HTML string.

How does IronPDF help in styling PDF forms using CSS?

IronPDF allows the use of CSS to style PDF forms. By embedding CSS in the HTML, you can customize the appearance and layout of the form fields, ensuring they match your desired design.

What steps are involved in saving a generated PDF form using this library?

After creating a PDF form with IronPDF, you can save it by using the saveAs method, which lets you specify the file name and location for the generated PDF document.

Can JavaScript be used within PDF forms created by IronPDF?

Yes, IronPDF supports the integration of JavaScript within HTML for PDF forms, allowing you to add interactive and dynamic elements to your forms, depending on your environment.

What is the role of the setLicenseKey method in using this library?

The License.setLicenseKey method in IronPDF is used to input your license key, which enables all features of the library without limitations.

How do you handle radio buttons in PDF forms using IronPDF?

In IronPDF, radio buttons are grouped to form a single interactive object within a PDF form. The selected option's value is captured, and you can manage these fields using the getForm and getFields methods.

What method is used for converting HTML to PDF in IronPDF?

The renderHtmlAsPdf method is used in IronPDF to convert HTML strings into PDF documents, allowing for dynamic content to be efficiently transformed into a PDF format.

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

Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?