DEV Community

loading...

Create Form Fields in Word in Java

eiceblue profile image E-iceblue Product Family Updated on ・3 min read

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.

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

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");
        titleText.getCharacterFormat().setFontName("Calibri");
        titleText.getCharacterFormat().setFontSize(16f);
        title.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //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.isHeader(true);
        headerRow.getRowFormat().setBackColor(new Color(0x00, 0x71, 0xb6));
        headerRow.getCells().get(0).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
        Paragraph headerParagraph = headerRow.getCells().get(0).addParagraph();
        TextRange headerText = headerParagraph.appendText("Personal Information");
        headerText.getCharacterFormat().setBold(true);

        //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);
        checkboxForm.setCheckBoxSize(8);

        //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);
        dropdownField.getDropDownItems().add("Male");
        dropdownField.getDropDownItems().add("Female");

        for (int i = 0; i < table.getRows().getCount(); i++) {
            //Set row height
            table.getRows().get(i).setHeight(20f);
            for (Object cell:table.getRows().get(i).getCells()){
                if (cell instanceof TableCell)
                {
                    //Set cell alignment
                    ((TableCell) cell).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                }
            }
        }

        //Set table alignment
        table.getTableFormat().setHorizontalAlignment(RowAlignment.Center);

        //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);
    }
}

Output:
Add Word form fields

Discussion (0)

pic
Editor guide