Create Fillable Forms in Word in Java

Fillable forms are often used in PDF files to collect user information. In some cases, you may also need to create interactive digital forms in Word documents so that others can fill out on these forms with necessary information before sending the document back to you. This article will share how to insert fillable forms in a Word document using Free Spire.Doc for Java library.

Install the Library (Two Methods)


Sample Code

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.CheckBoxFormField;
import com.spire.doc.fields.DropDownFormField;
import com.spire.doc.fields.TextFormField;

public class CreateFormFields {

public static void main(String[] args) {

//create a Word document and add a section
Document doc = new Document();
Section section = doc.addSection();

//add a table
Table table = section.addTable();

//add text to the cells of the first column
Paragraph paragraph = table.getRows().get(0).getCells().get(0).addParagraph();
paragraph.appendText("Text Form Field");
paragraph = table.getRows().get(1).getCells().get(0).addParagraph();
paragraph.appendText("Check Box Form Field");
paragraph = table.getRows().get(2).getCells().get(0).addParagraph();
paragraph.appendText("Drop Down Form Field");

//add a text form to the specific cell
paragraph = table.getRows().get(0).getCells().get(1).addParagraph();
TextFormField textField = (TextFormField) paragraph.appendField("textbox", FieldType.Field_Form_Text_Input);

//add a checkbox form to the specific cell
paragraph = table.getRows().get(1).getCells().get(1).addParagraph();
CheckBoxFormField checkboxField = (CheckBoxFormField)paragraph.appendField("checkbox", FieldType.Field_Form_Check_Box);

//add a dropdown-list form to the specific cell
paragraph = table.getRows().get(2).getCells().get(1).addParagraph();
DropDownFormField dropdownField = (DropDownFormField)paragraph.appendField("listbox",FieldType.Field_Form_Drop_Down);
dropdownField.getDropDownItems().add("United States");

//create a ParagraphStyle object
ParagraphStyle style = new ParagraphStyle(doc);

for (int i = 0; i < table.getRows().getCount(); i++) {

//set row height
for (Object cell:table.getRows().get(i).getCells()){
if (cell instanceof TableCell)
//set the vertical alignment of each cell to middle
((TableCell) cell).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);

//apply paragraph style to each cell
((TableCell) cell).getParagraphs().get(0).applyStyle(style.getName());

//save to file
doc.saveToFile("AddFormFields.docx", FileFormat.Docx_2013);



Sharing Java Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store