使用内容控制SDT

在Microsoft Word中,您可以通过从模板开始并添加内容控件(包括复选框、文本框、日期选取器和下拉列表)来创建表单。 在Aspose.Words中,来自加载到Aspose.Words中的任何文档的结构化文档标记或内容控件作为StructuredDocumentTag节点导入。 结构化文档标签(SDT或内容控件)允许将客户定义的语义及其行为和外观嵌入到文档中。 StructuredDocumentTag可以在以下位置出现在文档中:

  • 块级-在段落和表格中,作为正文、HeaderFooter、注释、脚注或形状节点的子级
  • 行级别-表中的行之间,作为表节点的子级
  • 单元格级别-在表行中的单元格之间,作为行节点的子节点
  • 内联级别-内联内容之间,作为段落的子级
  • 嵌套在另一个StructuredDocumentTag内

如何设置样式以格式化输入到内容控件中的文本

如果要设置内容控件的样式,可以使用StructuredDocumentTag.StyleStructuredDocumentTag.StyleName属性。 当您在输出文档的内容控件中键入文本时,键入的文本将具有样式"Quote"。

下面的代码示例演示如何设置内容控件的样式:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"input.docx");
System::SharedPtr<StructuredDocumentTag> sdt = System::DynamicCast<StructuredDocumentTag>(doc->GetChild(NodeType::StructuredDocumentTag, 0, true));
System::SharedPtr<Style> style = doc->get_Styles()->idx_get(StyleIdentifier::Quote);
sdt->set_Style(style);
System::String outputPath = outputDataDir + u"WorkingWithSDT.SetContentControlStyle.docx";
// Save the document to disk.
doc->Save(outputPath);

使用重复部分内容控制

重复部分内容控件允许重复其中包含的内容。 使用Aspose.Words,可以创建重复部分和重复部分项类型的结构化文档标记节点,为此,SdtType枚举类型提供RepeatingSectionItem属性。

下面的代码示例演示如何将重复部分内容控件绑定到表:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::String xml = System::String(u"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>") +
u"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
u"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>";
System::SharedPtr<CustomXmlPart> xmlPart = doc->get_CustomXmlParts()->Add(u"Books", xml);
System::SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Title");
builder->InsertCell();
builder->Write(u"Author");
builder->EndRow();
builder->EndTable();
System::SharedPtr<StructuredDocumentTag> repeatingSectionSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::RepeatingSection, Aspose::Words::Markup::MarkupLevel::Row);
repeatingSectionSdt->get_XmlMapping()->SetMapping(xmlPart, u"/books[1]/book", u"");
table->AppendChild(repeatingSectionSdt);
System::SharedPtr<StructuredDocumentTag> repeatingSectionItemSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::RepeatingSectionItem, Aspose::Words::Markup::MarkupLevel::Row);
repeatingSectionSdt->AppendChild(repeatingSectionItemSdt);
System::SharedPtr<Row> row = System::MakeObject<Row>(doc);
repeatingSectionItemSdt->AppendChild(row);
System::SharedPtr<StructuredDocumentTag> titleSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::PlainText, Aspose::Words::Markup::MarkupLevel::Cell);
titleSdt->get_XmlMapping()->SetMapping(xmlPart, u"/books[1]/book[1]/title[1]", u"");
row->AppendChild(titleSdt);
System::SharedPtr<StructuredDocumentTag> authorSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::PlainText, Aspose::Words::Markup::MarkupLevel::Cell);
authorSdt->get_XmlMapping()->SetMapping(xmlPart, u"/books[1]/book[1]/author[1]", u"");
row->AppendChild(authorSdt);
doc->Save(outputDataDir + u"WorkingWithSDT.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");