This article demonstrates how to insert fillable forms such as text boxes, check boxes and drop-down lists to a Word document using Spire.Doc for Java.
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(); table.resetCells(3,2); //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); textField.setTextFieldType(TextFormFieldType.Regular_Text); //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("Canada"); dropdownField.getDropDownItems().add("United States"); dropdownField.getDropDownItems().add("Other"); //create a ParagraphStyle object ParagraphStyle style = new ParagraphStyle(doc); style.setName("newFont"); style.getCharacterFormat().setFontName("Calibri"); style.getCharacterFormat().setFontSize(13); doc.getStyles().add(style); for (int i = 0; i < table.getRows().getCount(); i++) { //set row height table.getRows().get(i).setHeight(30f); 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("output/AddFormFields.docx", FileFormat.Docx_2013); } }