设为首页 加入收藏

TOP

如何通过Java代码在Word中创建可填充表单(一)
2023-07-25 21:36:52 】 浏览:89
Tags:何通过 Java Word 充表单

有时候,我们需要制作一个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() 方法保存文档。

完整代码

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 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇记录监控摄像头的接入过程及web端.. 下一篇为什么Tomcat架构要这么设计?这..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目