通过Java实现在Word中创建可填充表单
作者:Carina-baby 发布时间:2023-08-05 21:11:40
有时候,我们需要制作一个Word模板文档,然后发给用户填写,但我们希望用户只能在指定位置填写内容,其他内容不允许编辑和修改。这时候我们就可以通过表单控件来轻松实现这一功能。本文将为您介绍如何通过Java代码,以编程方式在Word中创建可填充表单。下面是我整理的步骤及方法,并附上Java代码供大家参考。
程序环境
方法1:手动引入。将 Free Spire.Doc for Java 下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序
方法2: 如果您想通过 Maven安装,则可以在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
在Word中创建可填充表单
用户打开下面的生成文档,只能编辑表格中的窗体,不能修改其他内容。详细步骤如下:
创建Document对象。
使用 Document.addSection() 方法添加一个节。
使用 Section.addTable() 方法添加表格。
使用 TableCell.addParagraph() 方法将段落添加到特定的表格单元格。
创建 StructureDocumentTagInline 类的实例,并使用 Paragraph.getChildObjects().add() 方法将其作为子对象添加到段落中。
使用 StructureDocumentTagInline 对象的 SDTProperties 属性和 SDTContent 属性下的方法指定结构化文档标记的属性和内容。结构化文档标签的类型可通过 SDTProperties.setSDTType() 方法设置。
使用 Document.protect() 方法防止用户编辑表单域之外的内容。
使用 Document.saveToFile() 方法保存文档。
完整代码
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);
//向单元格添加图片内容控件 (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("图片2.jpg");
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");
//保存结果文档
doc.saveToFile("WordForm.docx", FileFormat.Docx_2013);
}
}
效果图
来源:https://www.cnblogs.com/Carina-baby/p/17240036.html


猜你喜欢
- 面试题1:说说什么分布式事务?解释一下什么是CAP?现在互联网开发多使用微服务架构,一个简单的操作,在服务端可能就是由多个服务和数据库实例协
- 前言在服务器上,当我们启动了tomcat,就可以以http://ip地址:8080/文件路径/文件名的方式,进行访问到我们服务器上处于tom
- 这节主要完成一些基本的增删改查以及Service、Dao和Action的抽取。1. Service层的抽取  
- 记得老师讲课的时候,经常会用PPT遥控翻页笔来遥控幻灯片来给我们讲课,当时觉得非常有趣,由于这段时间接触了VSTO相关的开发,了解到了Off
- @AutoConfiguration读取所有jar包下的 /META-INF/spring.factories 并追加到一个 LinkedM
- 前言上文讲的MyBatis部署运行且根据官网运行了一个demo:一步到位部署运行MyBatis3源码<保姆级>jdbc再贴一个J
- MyBatis框架提供了二级缓存接口,我们只需要实现它再开启配置就可以使用了。特别注意,我们要解决缓存穿透、缓存穿透和缓存雪崩的问题,同时也
- 本文研究的主要是java中的null“类型”的相关实例,具体介绍如下。先给出一道简单的null相关的题目,引发我们对null的探讨,后面会根
- 目录位运算按位“与” &按位“或” |异或 ^移位运算左移 <<右移 >>无符号右移 >>>
- 最近在公司,项目不是很忙了,偶尔看见一个兄台在CSDN求助,帮忙要一个自定义的渐变色进度条,我当时看了一下进度条,感觉挺漂亮的,就尝试的去自
- 首先介绍下JSON的定义,JSON是JavaScript Object Notation的缩写。一种轻量级的数据交换格式,具有良好的可读和便
- RequestBody注解的List参数传递Controller方法参数:@RequestBody List<Long> ids
- 废话目前流行的前后端分离让Java程序员可以更加专注的做好后台业务逻辑的功能实现,提供如返回Json格式的数据接口就可以。SpringBoo
- 一、为什么会存在动态内存int data=20;//在栈空间上开辟4个字节空间char ch[5]={0};//在栈开辟5个字节连续空间上面
- 字符串采用unicode编码的方式时,计算字符串长度的方法找出UNICODE编码中的汉字的代表的范围“\u4E00” 到“\u9FBB”之间
- 一、安装MongoDB4.0.3(××)1.1、官方安装文档https://docs.mongodb.com/manual/tutorial
- if判断integer的问题昨天在使用mybatis的if判断integer时遇见一个小问题:<if test="isCho
- 在许多游戏中当我们因为一些问题无法接着进行游玩,我们都会选择保存,以便后面有空时,接着游玩。接下来,我们会学习一些Unity有关的存储方法。
- 在android 6.0中google终于给android系统加上了指纹识别的支持,这个功能在iPhone上早就已经实现了,并且在很多厂商的
- 1. Spring ProfileSpring可使用Profile绝对程序在不同环境下执行情况,包含配置、加载Bean、依赖等。 Sprin