#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//创建Document的对象
intrusive_ptr doc = new Document();
//添加一个节
intrusive_ptr section = doc->AddSection();
//添加一个表格
intrusive_ptr table = section->AddTable(true);
table->ResetCells(7, 2);
//添加文本到第一列的单元格
intrusive_ptr paragraph = table->GetRows()->GetItemInRowCollection(0)->GetCells()->GetItemInCellCollection(0)->AddParagraph();
paragraph->AppendText(L""纯文本内容控件"");
paragraph = table->GetRows()->GetItemInRowCollection(1)->GetCells()->GetItemInCellCollection(0)->AddParagraph();
paragraph->AppendText(L""格式文本内容控件"");
paragraph = table->GetRows()->GetItemInRowCollection(2)->GetCells()->GetItemInCellCollection(0)->AddParagraph();
paragraph->AppendText(L""图片内容控件"");
paragraph = table->GetRows()->GetItemInRowCollection(3)->GetCells()->GetItemInCellCollection(0)->AddParagraph();
paragraph->AppendText(L""下拉列表内容控件"");
paragraph = table->GetRows()->GetItemInRowCollection(4)->GetCells()->GetItemInCellCollection(0)->AddParagraph();
paragraph->AppendText(L""复选框内容控件"");
paragraph = table->GetRows()->GetItemInRowCollection(5)->GetCells()->GetItemInCellCollection(0)->AddParagraph();
paragraph->AppendText(L""组合框内容控件"");
paragraph = table->GetRows()->GetItemInRowCollection(6)->GetCells()->GetItemInCellCollection(0)->AddParagraph();
paragraph->AppendText(L""日期选取器内容控件"");
//添加纯文本内容控件到单元格(0,1)
paragraph = table->GetRows()->GetItemInRowCollection(0)->GetCells()->GetItemInCellCollection(1)->AddParagraph();
intrusive_ptr sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::Text);
sdt->GetSDTProperties()->SetAlias(L""纯文本"");
sdt->GetSDTProperties()->SetTag(L""纯文本"");
sdt->GetSDTProperties()->SetIsShowingPlaceHolder(true);
intrusive_ptr text = new SdtText(true);
text->SetIsMultiline(false);
sdt->GetSDTProperties()->SetControlProperties(text);
intrusive_ptr tr = new TextRange(doc);
tr->SetText(L""单击或点击此处输入文字。"");
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//添加格式文本内容控件到单元格(1,1)
paragraph = table->GetRows()->GetItemInRowCollection(1)->GetCells()->GetItemInCellCollection(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::RichText);
sdt->GetSDTProperties()->SetAlias(L""格式文本"");
sdt->GetSDTProperties()->SetTag(L""格式文本"");
sdt->GetSDTProperties()->SetIsShowingPlaceHolder(true);
text = new SdtText(true);
text->SetIsMultiline(false);
sdt->GetSDTProperties()->SetControlProperties(text);
tr = new TextRange(doc);
tr->SetText(L""单击或点击此处输入文字。"");
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//添加图片内容控件到单元格(2,1)
paragraph = table->GetRows()->GetItemInRowCollection(2)->GetCells()->GetItemInCellCollection(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::Picture);
sdt->GetSDTProperties()->SetAlias(L""图片"");
sdt->GetSDTProperties()->SetTag(L""图片"");
intrusive_ptr sdtPicture = new SdtPicture();
sdt->GetSDTProperties()->SetControlProperties(sdtPicture);
intrusive_ptr pic = new DocPicture(doc);
pic->LoadImageSpire(L""C:/Workspace.png"");
sdt->GetSDTContent()->GetChildObjects()->Add(pic);
//添加下拉列表内容控件到单元格(3,1)
paragraph = table->GetRows()->GetItemInRowCollection(3)->GetCells()->GetItemInCellCollection(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
sdt->GetSDTProperties()->SetSDTType(SdtType::DropDownList);
sdt->GetSDTProperties()->SetAlias(L""下拉列表"");
sdt->GetSDTProperties()->SetTag(L""下拉列表"");
paragraph->GetChildObjects()->Add(sdt);
intrusive_ptr sddl = new SdtDropDownList();
sddl->GetListItems()->Add(new SdtListItem(L""选择一项。"", L""1""));
sddl->GetListItems()->Add(new SdtListItem(L""项目 2"", L""2""));
sddl->GetListItems()->Add(new SdtListItem(L""项目 3"", L""3""));
sddl->GetListItems()->Add(new SdtListItem(L""项目 4"", L""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()->GetItemInRowCollection(4)->GetCells()->GetItemInCellCollection(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::CheckBox);
intrusive_ptr scb = new SdtCheckBox();
sdt->GetSDTProperties()->SetControlProperties(scb);
tr = new TextRange(doc);
sdt->GetChildObjects()->Add(tr);
scb->SetChecked(false);
paragraph->AppendText(L"" 选项 1"");
paragraph = table->GetRows()->GetItemInRowCollection(4)->GetCells()->GetItemInCellCollection(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(L"" 选项 2"");
//添加组合框内容控件到单元格(5,1)
paragraph = table->GetRows()->GetItemInRowCollection(5)->GetCells()->GetItemInCellCollection(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::ComboBox);
sdt->GetSDTProperties()->SetAlias(L""组合框"");
sdt->GetSDTProperties()->SetTag(L""组合框"");
intrusive_ptr cb = new SdtComboBox();
cb->GetListItems()->Add(new SdtListItem(L""选择一项。""));
cb->GetListItems()->Add(new SdtListItem(L""项目 2""));
cb->GetListItems()->Add(new SdtListItem(L""项目 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()->GetItemInRowCollection(6)->GetCells()->GetItemInCellCollection(1)->AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph->GetChildObjects()->Add(sdt);
sdt->GetSDTProperties()->SetSDTType(SdtType::DatePicker);
sdt->GetSDTProperties()->SetAlias(L""日期选取器"");
sdt->GetSDTProperties()->SetTag(L""日期选取器"");
intrusive_ptr date = new SdtDate();
date->SetCalendarType(CalendarType::Default);
date->SetDateFormatSpire(L""yyyy.MM.dd"");
date->SetFullDate(DateTime::GetNow());
sdt->GetSDTProperties()->SetControlProperties(date);
tr = new TextRange(doc);
tr->SetText(L""单击或点击此处输入日期。"");
sdt->GetSDTContent()->GetChildObjects()->Add(tr);
//设置仅允许用户编辑表单域
doc->Protect(ProtectionType::AllowOnlyFormFields, L""password"");
//保存文档
doc->SaveToFile(L""Output/表单.docx"", FileFormat::Docx2013);
doc->Close();
}

申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。