Word 允许用户创建可供其他人录入信息的表单。表单可以从多个对象收集数据或反馈,并且能够保持收集到的内容格式一致。以下是在 Word 文档中创建可填充表单可能需要用到的工具:
- 内容控件:供用户录入信息的表单区域。
- 表格:用于对齐表单中的文本和表单域,形成表单的边框和单元格等元素。
- 保护工具:允许用户填充表单域,但不能改变文档的其他内容。
Word 中的内容控件是内容容器,用户可以使用它们来创建结构化文档。结构化文档控制内容在文档中出现的位置。Word 2013 中有10种常见的内容控件,本文主要介绍如何使用 Spire.Doc for C++ 创建以下7种内容控件组成的可填充表单:
| 内容控件 | 描述 |
| 纯文本内容控件 | 允许用户输入无格式的文本。 |
| 格式文本内容控件 | 允许用户录入带格式的文本,以及表格、图片、内容控件等其他内容。 |
| 图片内容控件 | 允许用户录入单张图片。 |
| 下拉列表内容控件 | 允许用户从预定义的列表中选择项目。 |
| 组合框内容控件 | 允许用户从预定义的列表中选择项目或自己输入信息。 |
| 复选框内容控件 | 允许用户进行勾选。 |
| 日期选取器内容控件 | 允许用户从日历中选取日期。 |
安装 Spire.Doc for C++
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.Doc for C++ 集成到 C++ 程序中
在 Word 文档中创建可填充表单域
Spire.Doc for C++ 提供的 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() 方法保存文档。
- C++
#include <iostream>
#include "Spire/Doc/pch.h"
#ifdef LoadImage
#undef LoadImage
#endif
#define LoadImage LoadImageA
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/Collections/CellCollection.h"
#include "Spire/Doc/Collections/DocumentObjectCollection.h"
#include "Spire/Doc/Collections/RowCollection.h"
#include "Spire/Doc/Document.h"
#include "Spire/Doc/Documents/CalendarType.h"
#include "Spire/Doc/Documents/Paragraph.h"
#include "Spire/Doc/Documents/SDTInlineContent.h"
#include "Spire/Doc/Documents/SDTProperties.h"
#include "Spire/Doc/Documents/SdtCheckBox.h"
#include "Spire/Doc/Documents/SdtComboBox.h"
#include "Spire/Doc/Documents/SdtDate.h"
#include "Spire/Doc/Documents/SdtDropDownList.h"
#include "Spire/Doc/Documents/SdtListItem.h"
#include "Spire/Doc/Documents/SdtListItemCollection.h"
#include "Spire/Doc/Documents/SdtPicture.h"
#include "Spire/Doc/Documents/SdtText.h"
#include "Spire/Doc/Documents/SdtType.h"
#include "Spire/Doc/Documents/StructureDocumentTagInline.h"
#include "Spire/Doc/Fields/DocPicture.h"
#include "Spire/Doc/Fields/TextRange.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/ProtectionType.h"
#include "Spire/Doc/Section.h"
#include "Spire/Doc/Table.h"
#include "Spire/Doc/TableCell.h"
#include "Spire/Doc/TableRow.h"
#include "Spire/Doc/common/h/DateTime.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Collections;
using namespace Spire::Doc::Common;
using namespace Spire::Doc::Documents;
using namespace Spire::Doc::Fields;
int main() {
//创建Document的对象
intrusive_ptr<Document> doc = new Document();
//添加一个节
intrusive_ptr<Section> section = doc->AddSection();
//添加一个表格
intrusive_ptr<Table> table = section->AddTable(true);
table->ResetCells(7, 2);
//添加文本到第一列的单元格
intrusive_ptr<Paragraph> paragraph = table->GetRows()->GetItem(0)->GetCells()->GetItem(0)->AddParagraph();
paragraph->AppendText("纯文本内容控件");
paragraph = table->GetRows()->GetItem(1)->GetCells()->GetItem(0)->AddParagraph();
paragraph->AppendText("格式文本内容控件");
paragraph = table->GetRows()->GetItem(2)->GetCells()->GetItem(0)->AddParagraph();
paragraph->AppendText("图片内容控件");
paragraph = table->GetRows()->GetItem(3)->GetCells()->GetItem(0)->AddParagraph();
paragraph->AppendText("下拉列表内容控件");
paragraph = table->GetRows()->GetItem(4)->GetCells()->GetItem(0)->AddParagraph();
paragraph->AppendText("复选框内容控件");
paragraph = table->GetRows()->GetItem(5)->GetCells()->GetItem(0)->AddParagraph();
paragraph->AppendText("组合框内容控件");
paragraph = table->GetRows()->GetItem(6)->GetCells()->GetItem(0)->AddParagraph();
paragraph->AppendText("日期选取器内容控件");
//添加纯文本内容控件到单元格(0,1)
paragraph = table->GetRows()->GetItem(0)->GetCells()->GetItem(1)->AddParagraph();
intrusive_ptr<StructureDocumentTagInline> sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::Text);
sdt->GetSDTProperties()->SetAlias("纯文本");
sdt->GetSDTProperties()->SetTag("纯文本");
sdt->GetSDTProperties()->SetIsShowingPlaceHolder(true);
intrusive_ptr<SdtText> text = new SdtText(true);
text->SetIsMultiline(false);
sdt->GetSDTProperties()->SetControlProperties(text);
intrusive_ptr<TextRange> tr = new TextRange(doc);
tr->SetText("单击或点击此处输入文字。");
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//添加格式文本内容控件到单元格(1,1)
paragraph = table->GetRows()->GetItem(1)->GetCells()->GetItem(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::RichText);
sdt->GetSDTProperties()->SetAlias("格式文本");
sdt->GetSDTProperties()->SetTag("格式文本");
sdt->GetSDTProperties()->SetIsShowingPlaceHolder(true);
text = new SdtText(true);
text->SetIsMultiline(false);
sdt->GetSDTProperties()->SetControlProperties(text);
tr = new TextRange(doc);
tr->SetText("单击或点击此处输入文字。");
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//添加图片内容控件到单元格(2,1)
paragraph = table->GetRows()->GetItem(2)->GetCells()->GetItem(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::Picture);
sdt->GetSDTProperties()->SetAlias("图片");
sdt->GetSDTProperties()->SetTag("图片");
intrusive_ptr<SdtPicture> sdtPicture = new SdtPicture();
sdt->GetSDTProperties()->SetControlProperties(sdtPicture);
intrusive_ptr<DocPicture> pic = new DocPicture(doc);
pic->LoadImage("https://cdn.e-iceblue.cn/Workspace.png");
sdt->GetSDTContent()->GetChildObjects()->Add(pic);
//添加下拉列表内容控件到单元格(3,1)
paragraph = table->GetRows()->GetItem(3)->GetCells()->GetItem(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
sdt->GetSDTProperties()->SetSDTType(SdtType::DropDownList);
sdt->GetSDTProperties()->SetAlias("下拉列表");
sdt->GetSDTProperties()->SetTag("下拉列表");
paragraph->GetChildObjects()->Add(sdt);
intrusive_ptr<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()->GetItem(0)->GetDisplayText());
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//添加两个复选框内容控件到单元格(4,1)
paragraph = table->GetRows()->GetItem(4)->GetCells()->GetItem(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::CheckBox);
intrusive_ptr<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()->GetItem(4)->GetCells()->GetItem(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::CheckBox);
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()->GetItem(5)->GetCells()->GetItem(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::ComboBox);
sdt->GetSDTProperties()->SetAlias("组合框");
sdt->GetSDTProperties()->SetTag("组合框");
intrusive_ptr<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()->GetItem(0)->GetDisplayText());
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//添加日期选取器内容控件到单元格(6,1)
paragraph = table->GetRows()->GetItem(6)->GetCells()->GetItem(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::DatePicker);
sdt->GetSDTProperties()->SetAlias("日期选取器");
sdt->GetSDTProperties()->SetTag("日期选取器");
intrusive_ptr<SdtDate> date = new SdtDate();
date->SetCalendarType(CalendarType::Default);
date->SetDateFormat("yyyy.MM.dd");
date->SetFullDate(DateTime::GetNow());
sdt->GetSDTProperties()->SetControlProperties(date);
tr = new TextRange(doc);
tr->SetText("单击或点击此处输入日期。");
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//设置仅允许用户编辑表单域
doc->Protect(ProtectionType::AllowOnlyFormFields, "password");
//保存文档
doc->SaveToFile("Output/表单.docx", FileFormat::Docx2013);
doc->Close();
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







