Word 允许您创建其他人可以用来输入信息的表单。可填充表单可用于多种用途。人力资源部使用表格来收集员工和顾问信息;市场营销部门使用表格来调查客户对其产品和服务的满意度;组织机构使用表格来注册成员、学生或客户。创建表单时将使用的一些工具包括:
- 内容控件:用户在表单中输入信息的区域。
- 表格:表格在表单中用于对齐文本和表单域,以及创建边框和文本框。
- 保护:允许用户填充域,但不能更改文档的其余部分。
Word 中的内容控件是内容容器,用户可以使用它们来创建结构化文档。结构化文档控制内容在文档中出现的位置。Word 2013 中有十种类型的内容控件可用。本文将重点介绍如何使用 Spire.Doc for Java 在 Word 中创建一个可填充的表单,该表单由以下七个常见的内容控件组成。
| 内容控件 | 描述 | 
| 纯文本 | 仅限于纯文本的文本域,因此不能包含任何格式。 | 
| 富文本 | 一个文本域,可以包含格式化的文本或其他项目,如表格、图片或其他内容控件。 | 
| 图片 | 接受单张图片。 | 
| 下拉列表 | 下拉列表显示预定义的项目列表供用户选择。 | 
| 组合框 | 组合框允许用户在列表中选择预定义的值,或在控件的文本框中键入自己的值。 | 
| 复选框 | 复选框提供了一个图形小部件,允许用户进行二进制选择:是(选中)或否(未选中)。 | 
| 日期选取器 | 包含一个日历控件,用户可以从中选择一个日期。 | 
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从 这个链接 下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
在 Word 中创建可填充表单
Spire.Doc for Java 提供的 StructureDocumentTagInline 类用于为段落中的内联级结构(DrawingML 对象、域等)创建结构化文档标签。该类下的 SDTProperties 属性和 SDTContent 属性用于指定当前结构化文档标签的属性和内容。以下是在 Word 中创建带有内容控件的可填写表单的详细步骤。
- 创建 Document 对象。
- 使用 Document.addSection() 方法添加一个节。
- 使用 Section.addTable() 方法添加表格。
- 使用 TableCell.addParagraph() 方法将段落添加到特定的表格单元格。
- 创建 StructureDocumentTagInline 类的实例,并使用 Paragraph.getChildObjects().add() 方法将其作为子对象添加到段落中。
- 使用 StructureDocumentTagInline 对象的 SDTProperties 属性和 SDTContent 属性下的方法指定结构化文档标记的属性和内容。结构化文档标签的类型可通过 SDTProperties.setSDTType() 方法设置。
- 使用 Document.protect() 方法防止用户编辑表单域之外的内容。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.util.Date;
public class CreateFillableForm {
    public static void main(String[] args) {
        //创建文档对象
        Document doc = new Document();
        //添加一个节
        Section section = doc.addSection();
        //添加一个表格
        Table table = section.addTable(true);
        table.resetCells(7, 2);
        //将文本添加到第一列的单元格
        Paragraph paragraph = table.getRows().get(0).getCells().get(0).addParagraph();
        paragraph.appendText("纯文本内容控件");
        paragraph = table.getRows().get(1).getCells().get(0).addParagraph();
        paragraph.appendText("富文本内容控件");
        paragraph = table.getRows().get(2).getCells().get(0).addParagraph();
        paragraph.appendText("图片内容控件");
        paragraph = table.getRows().get(3).getCells().get(0).addParagraph();
        paragraph.appendText("下拉列表内容控件");
        paragraph = table.getRows().get(4).getCells().get(0).addParagraph();
        paragraph.appendText("复选框内容控件");
        paragraph = table.getRows().get(5).getCells().get(0).addParagraph();
        paragraph.appendText("组合框内容控件");
        paragraph = table.getRows().get(6).getCells().get(0).addParagraph();
        paragraph.appendText("日期选择器内容控件");
        //向单元格添加纯文本内容控件 (0,1)
        paragraph = table.getRows().get(0).getCells().get(1).addParagraph();
        StructureDocumentTagInline sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Text);
        sdt.getSDTProperties().setAlias("纯文本");
        sdt.getSDTProperties().setTag("纯文本");
        sdt.getSDTProperties().isShowingPlaceHolder(true);
        SdtText text = new SdtText(true);
        text.isMultiline(false);
        sdt.getSDTProperties().setControlProperties(text);
        TextRange tr = new TextRange(doc);
        tr.setText("单击或点击此处输入文本。");
        sdt.getSDTContent().getChildObjects().add(tr);
        //向单元格添加富文本内容控件 (1,1)
        paragraph = table.getRows().get(1).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Rich_Text);
        sdt.getSDTProperties().setAlias("富文本");
        sdt.getSDTProperties().setTag("富文本");
        sdt.getSDTProperties().isShowingPlaceHolder(true);
        text = new SdtText(true);
        text.isMultiline(false);
        sdt.getSDTProperties().setControlProperties(text);
        tr = new TextRange(doc);
        tr.setText("单击或点击此处输入文本。");
        sdt.getSDTContent().getChildObjects().add(tr);
        //Add a picture content control to the cell (2,1)
        paragraph = table.getRows().get(2).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Picture);
        sdt.getSDTProperties().setAlias("图片");
        sdt.getSDTProperties().setTag("图片");
        SdtPicture sdtPicture = new SdtPicture();
        sdt.getSDTProperties().setControlProperties(sdtPicture);
        DocPicture pic = new DocPicture(doc);
        pic.loadImage("C:\\Users\\Administrator\\Desktop\\图片.png");
        sdt.getSDTContent().getChildObjects().add(pic);
        //向单元格添加下拉列表内容控件(3,1)
        paragraph = table.getRows().get(3).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        sdt.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
        sdt.getSDTProperties().setAlias("下拉列表");
        sdt.getSDTProperties().setTag("下拉列表");
        paragraph.getChildObjects().add(sdt);
        SdtDropDownList sddl = new SdtDropDownList();
        sddl.getListItems().add(new SdtListItem("选择一个项目。", "1"));
        sddl.getListItems().add(new SdtListItem("项目2", "2"));
        sddl.getListItems().add(new SdtListItem("项目3", "3"));
        sddl.getListItems().add(new SdtListItem("项目4", "4"));
        sdt.getSDTProperties().setControlProperties(sddl);
        tr = new TextRange(doc);
        tr.setText(sddl.getListItems().get(0).getDisplayText());
        sdt.getSDTContent().getChildObjects().add(tr);
        //向单元格添加两个复选框内容控件 (4,1)
        paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
        SdtCheckBox scb = new SdtCheckBox();
        sdt.getSDTProperties().setControlProperties(scb);
        tr = new TextRange(doc);
        sdt.getChildObjects().add(tr);
        scb.setChecked(false);
        paragraph.appendText(" 选项 1");
        paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
        scb = new SdtCheckBox();
        sdt.getSDTProperties().setControlProperties(scb);
        tr = new TextRange(doc);
        sdt.getChildObjects().add(tr);
        scb.setChecked(false);
        paragraph.appendText(" 选项 2");
        //将组合框内容控件添加到单元格 (5,1)
        paragraph = table.getRows().get(5).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Combo_Box);
        sdt.getSDTProperties().setAlias("组合框");
        sdt.getSDTProperties().setTag("组合框");
        SdtComboBox cb = new SdtComboBox();
        cb.getListItems().add(new SdtListItem("选择一个项目."));
        cb.getListItems().add(new SdtListItem("项目 2"));
        cb.getListItems().add(new SdtListItem("项目 3"));
        sdt.getSDTProperties().setControlProperties(cb);
        tr = new TextRange(doc);
        tr.setText(cb.getListItems().get(0).getDisplayText());
        sdt.getSDTContent().getChildObjects().add(tr);
        //将日期选择器内容控件添加到单元格(6,1)
        paragraph = table.getRows().get(6).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Date_Picker);
        sdt.getSDTProperties().setAlias("日期选择器");
        sdt.getSDTProperties().setTag("日期选择器");
        SdtDate date = new SdtDate();
        date.setCalendarType(CalendarType.Default);
        date.setDateFormat("yyyy.MM.dd");
        date.setFullDate(new Date());
        sdt.getSDTProperties().setControlProperties(date);
        tr = new TextRange(doc);
        tr.setText("单击或轻按以输入日期。");
        sdt.getSDTContent().getChildObjects().add(tr);
        //仅允许用户编辑表单域
        doc.protect(ProtectionType.Allow_Only_Form_Fields, "permission-psd");
        //Save to file
        doc.saveToFile("WordForm.docx", FileFormat.Docx_2013);
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



