A form field is an area where users can fill in and select information by themselves. The most common form fields include text box, radio button, check box, list box, combo box, signature field and button. Therefore, this article will demonstrate how to add these fillable form fields to PDF by using Free Spire.PDF for Java.
Installation
Method 1: You need to download the Free Spire.PDF for Java and unzip it. And then add the Spire.Pdf.jar file to your project as dependency.
Method 2: If you use maven, you can easily add the jar dependency by adding the following configurations to the pom.xml.
<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.free</artifactId>
<version>2.6.3</version>
</dependency>
</dependencies>
Creating Fillable PDF Form Fields:
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 FormFields {
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("Sumbit");
buttonField.setFont(font);
doc.getForm().getFields().add(buttonField);
//save to file
doc.saveToFile("AddFormField.pdf", FileFormat.PDF);
}
}
Top comments (0)