Create Fillable Forms in Word in Java

·

2 min read

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 these forms with the 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)

Method 1: Download the free Java library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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.doc.free</artifactId>
      <version>5.2.0</version>
   </dependency>
</dependencies>

Sample Code

Free Spire.Doc for Java offers the Paragraph.appendField(String fieldName, com.spire.doc.FieldType fieldType) method for users to insert fillable forms such as text boxes, check boxes and drop-down lists to a Word document. The complete sample code is shown below.

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("AddFormFields.docx", FileFormat.Docx_2013);
    }
}