使用内容控制 SDT
在 Microsoft Word 中,您可以通过从模板开始并添加内容控件(包括复选框、文本框、日期选择器和下拉列表)来创建表单。在 Aspose.Words 中,加载到 Aspose.Words 中的任何文档中的结构化文档标签或内容控件都会作为 StructuredDocumentTag 节点导入。结构化文档标签(SDT 或内容控制)允许将客户定义的语义及其行为和外观嵌入到文档中。 StructuredDocumentTag 可以出现在文档中的以下位置:
- 块级 - 在段落和表格中,作为 Body、HeaderFooter、Comment、Footnote 或 Shape 节点的子节点
- 行级 - 在表中的行中,作为 Table 节点的子节点
- 单元格级别 - 在表格行的单元格中,作为 Row 节点的子节点
- 内联级别 - 在内联内容中,作为 Paragraph 的子级
- 嵌套在另一个 StructuredDocumentTag 中
将内容控件插入文档
在此版本的 Aspose.Words 中,可以创建以下类型的 SDT 或内容控件:
- Checkbox
- DropDownList
- ComboBox
- Date
- BuildingBlockGallery
- Group
Picture
- RichText
- PlainText
下面的代码示例演示了如何创建复选框类型的内容控件。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
sdtCheckBox = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.CHECKBOX, aw.markup.MarkupLevel.INLINE) | |
builder.insert_node(sdtCheckBox) | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.check_box_type_content_control.docx", aw.SaveFormat.DOCX) |
下面的代码示例演示了如何创建富文本框类型的内容控件。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
sdtRichText = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.RICH_TEXT, aw.markup.MarkupLevel.BLOCK) | |
para = aw.Paragraph(doc) | |
run = aw.Run(doc) | |
run.text = "Hello World" | |
run.font.color = drawing.Color.green | |
para.runs.add(run) | |
sdtRichText.child_nodes.add(para) | |
doc.first_section.body.append_child(sdtRichText) | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.rich_text_box_content_control.docx") |
下面的代码示例演示了如何创建组合框类型的内容控件。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
sdt = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.COMBO_BOX, aw.markup.MarkupLevel.BLOCK) | |
sdt.list_items.add(aw.markup.SdtListItem("Choose an item", "-1")) | |
sdt.list_items.add(aw.markup.SdtListItem("Item 1", "1")) | |
sdt.list_items.add(aw.markup.SdtListItem("Item 2", "2")) | |
doc.first_section.body.append_child(sdt) | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.combo_box_content_control.docx") |
如何更新内容控件
本节介绍如何以编程方式更新 SDT 或内容控制的值。
以下代码示例显示如何设置复选框的当前状态:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Structured document tags.docx") | |
# Get the first content control from the document. | |
sdtCheckBox = doc.get_child(aw.NodeType.STRUCTURED_DOCUMENT_TAG, 0, True).as_structured_document_tag() | |
if (sdtCheckBox.sdt_type == aw.markup.SdtType.CHECKBOX) : | |
sdtCheckBox.checked = True | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.current_state_of_check_box.docx") |
下面的代码示例展示了如何修改纯文本框、下拉列表和图片类型的内容控件:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Structured document tags.docx") | |
for sdt in doc.get_child_nodes(aw.NodeType.STRUCTURED_DOCUMENT_TAG, True) : | |
sdt = sdt.as_structured_document_tag() | |
if (sdt.sdt_type == aw.markup.SdtType.PLAIN_TEXT) : | |
sdt.remove_all_children() | |
para = sdt.append_child(aw.Paragraph(doc)).as_paragraph() | |
run = aw.Run(doc, "new text goes here") | |
para.append_child(run) | |
elif (sdt.sdt_type == aw.markup.SdtType.DROP_DOWN_LIST) : | |
secondItem = sdt.list_items[2] | |
sdt.list_items.selected_value = secondItem | |
elif (sdt.sdt_type == aw.markup.SdtType.PICTURE) : | |
shape = sdt.get_child(NodeType.shape, 0, True).as_shape() | |
if (shape.has_image) : | |
shape.image_data.set_image(docs_base.images_dir + "Watermark.png") | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.modify_content_controls.docx") |
将内容控制绑定到自定义 XML 部件
您可以将内容控件与 Word 文档中的 XML 数据(自定义 XML 部分)绑定。
以下代码示例演示如何将内容控件绑定到自定义 XML 部分:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
xmlPart = doc.custom_xml_parts.add(str(uuid.uuid4()), "<root><text>Hello, World!</text></root>") | |
sdt = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.PLAIN_TEXT, aw.markup.MarkupLevel.BLOCK) | |
doc.first_section.body.append_child(sdt) | |
sdt.xml_mapping.set_mapping(xmlPart, "/root[1]/text[1]", "") | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.bind_sd_tto_custom_xml_part.doc") |
结构化文档标记范围的 XMLMapping
您可以使用 StructuredDocumentTagRangeStart.xml_mapping 属性获取此结构化文档标记范围到当前文档的自定义 XML 部分中的 XML 数据的映射。然而,set_mapping 方法可用于将结构化文档标记范围映射到 XML 数据。
以下代码示例展示了如何设置 XML 映射:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Multi-section structured document tags.docx") | |
# Construct an XML part that contains data and add it to the document's CustomXmlPart collection. | |
xmlPartId = str(uuid.uuid4()) | |
xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>" | |
xmlPart = doc.custom_xml_parts.add(xmlPartId, xmlPartContent) | |
print(xmlPart.data.decode("utf-8")) | |
# Create a StructuredDocumentTag that will display the contents of our CustomXmlPart in the document. | |
sdtRangeStart = doc.get_child(aw.NodeType.STRUCTURED_DOCUMENT_TAG_RANGE_START, 0, True).as_structured_document_tag_range_start() | |
# If we set a mapping for our StructuredDocumentTag, | |
# it will only display a part of the CustomXmlPart that the XPath points to. | |
# This XPath will point to the contents second "<text>" element of the first "<root>" element of our CustomXmlPart. | |
sdtRangeStart.xml_mapping.set_mapping(xmlPart, "/root[1]/text[2]", None) | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.structured_document_tag_range_start_xml_mapping.docx") |
清除内容控件的内容
您可以通过显示占位符来清除内容控件的内容。 StructuredDocumentTag.clear 方法清除此结构化文档标记的内容并显示占位符(如果已定义)。但是,如果内容控件有修订,则无法清除该内容。如果内容控件没有占位符,则会像 MS Word 中一样插入五个空格(重复部分、重复部分项目、组、复选框、引文除外)。如果内容控件映射到自定义 XML,则引用的 XML 节点将被清除。
下面的代码示例展示了如何清除内容控件的内容:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Structured document tags.docx") | |
sdt = doc.get_child(aw.NodeType.STRUCTURED_DOCUMENT_TAG, 0, True).as_structured_document_tag() | |
sdt.clear() | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.clear_contents_control.doc") |
更改内容控件背景和边框颜色
StructuredDocumentTag.color 属性允许您获取或设置内容控件的颜色。颜色在两种情况下影响内容控制:
- 当鼠标移到内容控件上时,MS Word 会突出显示内容控件的背景。这有助于识别内容控件。突出显示的颜色比 color 稍微"柔和"一些。例如,当 color 为红色时,MS Word 用粉红色突出显示背景。
- 当您与内容控件交互(编辑、选取等)时,内容控件的边框将使用 color 着色。
以下代码示例展示了如何更改内容控件的颜色:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Structured document tags.docx") | |
sdt = doc.get_child(aw.NodeType.STRUCTURED_DOCUMENT_TAG, 0, True).as_structured_document_tag() | |
sdt.color = drawing.Color.red | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.set_content_control_color.docx") |
如何设置样式以格式化在内容控件中键入的文本
如果要设置内容控件的样式,可以使用StructuredDocumentTag.style或StructuredDocumentTag.style_name属性。当您在输出文档的内容控件中键入文本时,键入的文本将具有"引用"样式。
下面的代码示例展示了如何设置内容控件的样式:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Structured document tags.docx") | |
sdt = doc.get_child(aw.NodeType.STRUCTURED_DOCUMENT_TAG, 0, True).as_structured_document_tag() | |
style = doc.styles.get_by_style_identifier(aw.StyleIdentifier.QUOTE) | |
sdt.style = style | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.set_content_control_style.docx") |
使用重复部分内容控制
重复部分内容控件允许重复其中包含的内容。使用 Aspose.Words,可以创建重复节和重复节项类型的结构化文档标记节点,为此,SdtType 枚举类型提供了 REPEATING_SECTION_ITEM 属性。
以下代码示例演示如何将重复节内容控件绑定到表:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
xmlPart = doc.custom_xml_parts.add("Books", | |
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" + | |
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" + | |
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>") | |
table = builder.start_table() | |
builder.insert_cell() | |
builder.write("Title") | |
builder.insert_cell() | |
builder.write("Author") | |
builder.end_row() | |
builder.end_table() | |
repeatingSectionSdt =aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.REPEATING_SECTION, aw.markup.MarkupLevel.ROW) | |
repeatingSectionSdt.xml_mapping.set_mapping(xmlPart, "/books[1]/book", "") | |
table.append_child(repeatingSectionSdt) | |
repeatingSectionItemSdt = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.REPEATING_SECTION_ITEM, aw.markup.MarkupLevel.ROW) | |
repeatingSectionSdt.append_child(repeatingSectionItemSdt) | |
row = aw.tables.Row(doc) | |
repeatingSectionItemSdt.append_child(row) | |
titleSdt = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.PLAIN_TEXT, aw.markup.MarkupLevel.CELL) | |
titleSdt.xml_mapping.set_mapping(xmlPart, "/books[1]/book[1]/title[1]", "") | |
row.append_child(titleSdt) | |
authorSdt = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.PLAIN_TEXT, aw.markup.MarkupLevel.CELL) | |
authorSdt.xml_mapping.set_mapping(xmlPart, "/books[1]/book[1]/author[1]", "") | |
row.append_child(authorSdt) | |
doc.save(docs_base.artifacts_dir + "WorkingWithSdt.creating_table_repeating_section_mapped_to_custom_xml_part.docx") |