DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Updated on

Create Form Fields in Word in Java

You can create a template Word document with legacy form fields that others can fill in. Basically, there are three types of legacy form field types in Microsoft Word:

  1. Text Form Field
  2. Check Box Form Field
  3. Drop-Down Form Field

In this article, I will introduce how to create text, check box and drop-down form fields in a Word document in Java by using Free Spire.Doc for Java library.

Add Dependencies

First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that.
If you use maven, you need to add the following code to your project’s pom.xml file.


For non-maven projects, download Free Spire.Doc for Java pack from this website and add Spire.Doc.jar in the lib folder into your project as a dependency.

Create Form Fields

Free Spire.Doc for Java API provides a Paragraph.appendField method for creating form fields in Word. After the form fields are added, you can protect the document using Document.protect method to ensure the users can only fill out the form fields in the document. In the following example, you will see how to add form fields to a Word document and protect the document by using this API.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.CheckBoxFormField;
import com.spire.doc.fields.DropDownFormField;
import com.spire.doc.fields.TextFormField;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class CreateFormFields {
    public static void main(String[] args) throws Exception {
        //Create a Document instance
        Document doc = new Document();
        //Add a section
        Section section = doc.addSection();

        //Add a title paragraph
        Paragraph title = section.addParagraph();
        TextRange titleText = title.appendText("Employee Information");

        //Create a table
        Table table = section.addTable(true);
        table.resetCells(5, 2);

        //Merge cells in the first row
        table.applyHorizontalMerge(0, 0, 1);

        //Set table header
        TableRow headerRow = table.getRows().get(0);
        headerRow.getRowFormat().setBackColor(new Color(0x00, 0x71, 0xb6));
        Paragraph headerParagraph = headerRow.getCells().get(0).addParagraph();
        TextRange headerText = headerParagraph.appendText("Personal Information");

        //Add paragraph to table cell [1,0]
        Paragraph paragraph = table.getRows().get(1).getCells().get(0).addParagraph();
        TextRange textRange = paragraph.appendText("Full name");

        //Add paragraph to table cell [1,1] and append a text form field to the paragraph
        paragraph = table.getRows().get(1).getCells().get(1).addParagraph();
        TextFormField textForm = (TextFormField)paragraph.appendField("FullName", FieldType.Field_Form_Text_Input);
        //Add paragraph to table cell [2,0]
        paragraph = table.getRows().get(2).getCells().get(0).addParagraph();
        textRange = paragraph.appendText("Age");
        //Add paragraph to table cell [2,1] and append a text form field
        paragraph = table.getRows().get(2).getCells().get(1).addParagraph();
        textForm = (TextFormField)paragraph.appendField("Age", FieldType.Field_Form_Text_Input);

        //Add paragraph to table cell [3,0] 
        paragraph = table.getRows().get(3).getCells().get(0).addParagraph();
        textRange = paragraph.appendText("Married");
        //Add paragraph to table cell [3,1] and append a check box  form field
        paragraph = table.getRows().get(3).getCells().get(1).addParagraph();
        CheckBoxFormField checkboxForm = (CheckBoxFormField)paragraph.appendField("Married", FieldType.Field_Form_Check_Box);

        //Add paragraph to table cell [4,0] 
        paragraph = table.getRows().get(4).getCells().get(0).addParagraph();
        textRange = paragraph.appendText("Gender");
        //Add paragraph to table cell [4,1] and append a drop down  form field
        paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
        DropDownFormField dropdownField = (DropDownFormField)paragraph.appendField("DropDown",FieldType.Field_Form_Drop_Down);

        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 cell alignment
                    ((TableCell) cell).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);

        //Set table alignment

        //Protect the document so that users can only fill out the form fields
        doc.protect(ProtectionType.Allow_Only_Form_Fields, "password");

        //Save the result document
        doc.saveToFile("AddFormFields.docx", FileFormat.Docx_2013);

Add Word form fields

Top comments (0)