通过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
猜你喜欢
- BitArray的基础可以看菜鸟编程BitArray 类管理一个紧凑型的位值数组,它使用布尔值来表示,其中 true 表示位是开启的(1),
- SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-
- 仅供学习交流,禁止商业用途。如侵害利益,联系必删!前言最近一位小伙伴钟爱二次元文化,于是找到半次元这个app,但是很快他就遇到了问题。一、案
- 本文实例讲述了C#实现Base64处理的加密解密,编码解码。分享给大家供大家参考,具体如下:using System;using Syste
- 报错翻译: compileSdkVersion android-24”需要JDK 1.8或更高版本编译。报错现象如下图:原因:st
- 背景大家在使用Selenium + Chromedriver爬取网站信息的时候,以为这样就能做到不被网站的反爬虫机制发现。但是实际上很多参数
- 先看代码public class MaxHuiWen {public static void main(String[] args) { &
- 由于众所周知的原因,maven的库在中国大陆非常慢。我在百度上搜到的大部分文章都是直接在~/.m2/settings.xml 加入以下内容&
- 前后端分离开发中,一般都会遇到请求跨域问题。而且一般也会遇到登陆失效问题。今天就以springboot和vue为例来看如何解决上述问题增加过
- 1. 什么是对象池对象池,顾名思义就是一定数量的已经创建好的对象(Object)的集合。当需要创建对象时,先在池子中获取,如果池子中没有符合
- 在使用struts多模块的,找到一些小技巧和经验,与大家分享一下。 关于多module的配置就不说了,只需要用不同的config
- 本文实例讲述了Android编程实现WebView添加进度条的方法。分享给大家供大家参考,具体如下:标准的XML界面<?xml ver
- 一、前言在Spring中,事务有两种实现方式:编程式事务管理: 编程式事务管理使用TransactionTemplate可实现更细
- 1. strlen —— 求字符串长度1.1 strlen 的声明与用处strlen ,我们有一些英
- 1. 源码阅读环境搭建ide:IntelliJ IDEA 2020.1包管理:gradleeureka版本:1.10.11Spring Cl
- 一、 WillPopScope用法WillPopScope本质是一个widget用于拦截物理按键返回事件(Android的物理返回键和iOS
- 本文向您展示了在 Flutter 中实现完美的验证码输入框几种不同方法。重点是什么?真实世界的 完美的验证码输入框或 PIN 输入 UI 通
- 今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查、分页、排序、事务操作等功能。下面先来介绍一下JPA中一些常用的
- 1 SharedPreferences 介绍SharedPreferences是使用键值对的方式来存储数据的SharedPreference
- 在类中自定义的“函数”称为“方法”,由于C#是完全面向对象的