DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add Form Fields to PDF in Java

Pdf form fields allow users to interact with the document directly without printing or scanning. The most frequently used types include text box, combo box, check box, list box, etc. In this article, I am going to introduce how to add fillable form fields to a PDF document using Spire.PDF for Java.

Spire.PDF is a Java class library written to support the creation and manipulation of PDF documents. It offers a number of useful classes in the com.spire.pdf.fields namespace, allowing programmers to create various types of forms and handle form fields embedded in an existing PDF document. Here are some of them involved in this article.

Class Description
PdfTextBoxField Represents text box field in the PDF form.
PdfCheckBoxField Represents check box field in the PDF form.
PdfComboBoxField Represents combo box field in the PDF Form.
PdfListBoxField Represents list box field of the PDF form.
PdfListFieldItem Represents an item of the list fields.
PdfRadioButtonListField Represents radio button field in the PDF form.
PdfRadioButtonListItem Represents an item of a radio button list.
PdfButtonField Represents button field in the PDF form.

Install Spire.Pdf.jar

If you’re creating a non-Maven project, download the jar file from this link and add it as a dependency in your applicaiton. For Maven projects, you can easily add the jar in your applciation using the following configurations.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.pdf</artifactId>
        <version>3.8.2</version>
    </dependency>
</dependencies>

Using the code

import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;

public class AddFormFieldsToPdf {

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

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Add a page
        PdfPageBase page = doc.getPages().add();

        //Initialize x and y coordinates
        float baseX = 100;
        float baseY = 0;

        //Create brush objects
        PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
        PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.black));

        //Create font
        PdfFont font = new PdfFont(PdfFontFamily.Times_Roman, 12f, EnumSet.of(PdfFontStyle.Regular));

        //Add a text box to pdf
        String text = "Name:";
        page.getCanvas().drawString(text, font, brush1, new Point2D.Float(0, baseY));
        Rectangle2D.Float tbxBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);
        PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
        textBox.setBounds(tbxBounds);
        textBox.setFont(font);
        doc.getForm().getFields().add(textBox);
        baseY += 35;

        //Add radio buttons to pdf
        page.getCanvas().drawString("Gender:", font, brush1, new Point2D.Float(0, baseY));
        PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "radio");
        PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("male");
        radioItem1.setBounds(new Rectangle2D.Float(baseX, baseY, 15, 15));
        page.getCanvas().drawString("Male", font, brush2, new Point2D.Float(baseX + 20, baseY));
        PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("female");
        radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15));
        page.getCanvas().drawString("Female", font, brush2, new Point2D.Float(baseX + 90, baseY));
        radioButtonListField.getItems().add(radioItem1);
        radioButtonListField.getItems().add(radioItem2);
        radioButtonListField.setSelectedIndex(0);
        doc.getForm().getFields().add(radioButtonListField);
        baseY += 35;

        //Add a combo box to pdf
        page.getCanvas().drawString("Country:", font, brush1, new Point2D.Float(0, baseY));
        Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);
        PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "combobox");
        comboBoxField.setBounds(cmbBounds);
        comboBoxField.getItems().add(new PdfListFieldItem("United States", "us"));
        comboBoxField.getItems().add(new PdfListFieldItem("Canada", "can"));
        comboBoxField.getItems().add(new PdfListFieldItem("China", "cn"));
        comboBoxField.getItems().add(new PdfListFieldItem("Japan", "jpn"));
        comboBoxField.setSelectedIndex(0);
        comboBoxField.setFont(font);
        doc.getForm().getFields().add(comboBoxField);
        baseY += 35;

        //Add checkboxes to pdf
        page.getCanvas().drawString("Hobbies:", font, brush1, new Point2D.Float(0, baseY));
        java.awt.geom.Rectangle2D.Float rec1 = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 15, 15);
        PdfCheckBoxField checkBoxField = new PdfCheckBoxField(page, "travel");
        checkBoxField.setBounds(rec1);
        checkBoxField.setChecked(false);
        page.getCanvas().drawString("Travel", font, brush2, new Point2D.Float(baseX + 20, baseY));
        java.awt.geom.Rectangle2D.Float rec2 = new java.awt.geom.Rectangle2D.Float(baseX + 70, baseY, 15, 15);
        PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "game");
        checkBoxField1.setBounds(rec2);
        checkBoxField1.setChecked(false);
        page.getCanvas().drawString("Game", font, brush2, new Point2D.Float(baseX + 90, baseY));
        doc.getForm().getFields().add(checkBoxField);
        doc.getForm().getFields().add(checkBoxField1);
        baseY += 35;

        //Add a list box to pdf
        page.getCanvas().drawString("Degree:", font, brush1, new Point2D.Float(0, baseY));
        java.awt.geom.Rectangle2D.Float rec = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 150, 50);
        PdfListBoxField listBoxField = new PdfListBoxField(page, "degree");
        listBoxField.getItems().add(new PdfListFieldItem("High School", "high"));
        listBoxField.getItems().add(new PdfListFieldItem("College Degree", "college"));
        listBoxField.getItems().add(new PdfListFieldItem("Master's Degree", "master"));
        listBoxField.setBounds(rec);
        listBoxField.setFont(font);
        listBoxField.setSelectedIndex(0);
        doc.getForm().getFields().add(listBoxField);
        baseY += 75;

        //Add a button to pdf
        Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);
        PdfButtonField buttonField = new PdfButtonField(page, "submit");
        buttonField.setBounds(btnBounds);
        buttonField.setText("Submit");
        buttonField.setFont(font);
        doc.getForm().getFields().add(buttonField);

        //Save to file
        doc.saveToFile("AddFormFields.pdf", FileFormat.PDF);
    }
}

Output

Alt Text

Top comments (0)