PDF fillable forms are frequently
used to collect data and information of users and common form fields include
text box, radio button, check box, list box and combo box. This article will
introduce how to create a fillable PDF form using a free third-party library
called Free Spire.PDF for Java.
Before running Java codes, we
need to add a Jar file called Spire.Pdf.jar which is located in this library to
IDEA. You can get it from the official website, or reference it using the
following Maven configurations.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code
Here I’ll create a new PDF document and add fillable form fields to it.
package FormField;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;
public class CreateFormField {
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 textbox to pdf
String text = "TextBox:";
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.setText("Hello World");
textBox.setFont(font);
doc.getForm().getFields().add(textBox);
baseY +=25;
//add checkboxes to pdf
page.getCanvas().drawString("CheckBox:", 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, "checkbox1");
checkBoxField.setBounds(rec1);
checkBoxField.setChecked(false);
page.getCanvas().drawString("Option 1", 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, "checkbox2");
checkBoxField1.setBounds(rec2);
checkBoxField1.setChecked(false);
page.getCanvas().drawString("Option 2", font, brush2, new Point2D.Float(baseX+90, baseY));
doc.getForm().getFields().add(checkBoxField);
doc.getForm().getFields().add(checkBoxField1);
baseY += 25;
//add a listbox to pdf
page.getCanvas().drawString("ListBox:", 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, "listbox");
listBoxField.getItems().add(new PdfListFieldItem("Item 1", "item1"));
listBoxField.getItems().add(new PdfListFieldItem("Item 2", "item2"));
listBoxField.getItems().add(new PdfListFieldItem("Item 3", "item3"));;
listBoxField.setBounds(rec);
listBoxField.setFont(font);
listBoxField.setSelectedIndex(0);
doc.getForm().getFields().add(listBoxField);
baseY += 60;
//add radiobuttons to pdf
page.getCanvas().drawString("RadioButton:", font, brush1, new Point2D.Float(0, baseY));
PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "radio");
PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("option1");
radioItem1.setBounds(new Rectangle2D.Float(baseX, baseY, 15, 15));
page.getCanvas().drawString("Option 1", font, brush2, new Point2D.Float(baseX + 20, baseY));
PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("option2");
radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15));
page.getCanvas().drawString("Option 2", 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 += 25;
//add a combobox to pdf
page.getCanvas().drawString("ComboBox:", 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("Item 1", "item1"));
comboBoxField.getItems().add(new PdfListFieldItem("Item 2", "itme2"));
comboBoxField.getItems().add(new PdfListFieldItem("Item 3", "item3"));
comboBoxField.getItems().add(new PdfListFieldItem("Item 4", "item4"));
comboBoxField.setSelectedIndex(0);
comboBoxField.setFont(font);
doc.getForm().getFields().add(comboBoxField);
baseY += 25;
//add a signature field to pdf
page.getCanvas().drawString("Signature:", font, brush1, new Point2D.Float(0, baseY));
PdfSignatureField sgnField= new PdfSignatureField(page,"sgnField");
Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX, baseY, 150, 80);
sgnField.setBounds(sgnBounds);
doc.getForm().getFields().add(sgnField);
baseY += 90;
//add a button to pdf
page.getCanvas().drawString("Button:", font, brush1, new Point2D.Float(0, baseY));
Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);
PdfButtonField buttonField = new PdfButtonField(page, "button");
buttonField.setBounds(btnBounds);
buttonField.setText("Submit");
buttonField.setFont(font);
doc.getForm().getFields().add(buttonField);
//save to file
doc.saveToFile("output/AddFormField.pdf", FileFormat.PDF);
}
}
The screenshot below shows output PDF document containing form fields.