Werk met Inhoud Beheer SDT
In Microsoft Word kan jy’n vorm skep deur met’n sjabloon te begin en inhoudskontroles by te voeg, insluitend checkboxes, tekskassies, datumplukkers en aftreklyste. In Aspose.Words word’n Gestruktureerde Dokumentetiket of inhoudbeheer van enige dokument wat in Aspose.Words gelaai is, ingevoer as’n StructuredDocumentTag - knooppunt. Gestruktureerde dokument etikette (SDT of inhoud beheer) toelaat inbed kliënt-gedefinieerde semantiek sowel as sy gedrag en voorkoms in’n dokument. StructuredDocumentTag kan in’n dokument voorkom in die volgende plekke:
- Blokvlak tussen paragrawe en tabelle, as kind van’n Body, HeaderFooter, Comment, Footnote of’n Shape knoop
- Ry-vlak-Tussen rye In’n tabel, as’n kind van’n Table knoop
- Selvlak-Onder selle In’n tabel ry, as’n kind van’n Row knoop
- Inline-vlak-Onder inline inhoud binne, as’n kind van’n Paragraph
- Geneste binne’n ander StructuredDocumentTag
Invoeging Van Inhoudkontroles in’n Dokument
In hierdie weergawe van Aspose.Words, kan die volgende tipes van SDT of inhoud beheer geskep word:
- Checkbox
- DropDownList
- ComboBox
- Datum
- BuildingBlockGallery
- Groep
Picture
- RichText
- PlainText
Die volgende kode voorbeeld demonstreer hoe om inhoud beheer van tipe checkbox te skep.
# 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) |
Die volgende kode voorbeeld demonstreer hoe om inhoud beheer van tipe ryk teks boks te skep.
# 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") |
Die volgende kode voorbeeld demonstreer hoe om inhoud beheer van die tipe combo boks te skep.
# 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") |
Hoe Om Inhoudskontroles op te dateer
Hierdie afdeling verduidelik hoe om die waardes van SDT of inhoud beheer programmaties te werk.
Die volgende kode voorbeeld toon hoe om die huidige toestand van die checkboxstel:
# 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") |
Die volgende kode voorbeeld toon hoe om inhoud kontroles van tipe gewone teks boks, drop-down lys en prentjie te verander:
# 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") |
Bindende Inhoud Beheer Om Persoonlike XML Dele
Jy kan inhoudsbeheer bind met XML data (custom XML part) in Word-dokumente.
Die volgende kode voorbeeld toon hoe om inhoud beheer bind om persoonlike XML dele:
# 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 Van Gestruktureerde Dokument Tag Reeks
Jy kan die kartering van hierdie gestruktureerde dokument tag reeks te XML data in’n persoonlike XML deel van die huidige dokument met behulp van die StructuredDocumentTagRangeStart.xml_mapping eiendom. Die set_mapping metode kan egter gebruik word om’n gestruktureerde dokument tag reeks te karteer om XML data.
Die volgende kode voorbeeld toon hoe om XML kartering stel:
# 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") |
Maak die inhoud van’n Inhoudsbeheer skoon
U kan die inhoud van’n inhoudsbeheer skoonmaak deur’n plekhouer te vertoon. StructuredDocumentTag.clear metode maak die inhoud van hierdie gestruktureerde dokument tag skoon en vertoon’n plekhouer as dit gedefinieer is. Dit is egter nie moontlik om die inhoud van’n inhoudskontrole skoon te maak as dit hersienings het nie. As’n inhoudskontrole geen plekhouer het nie, word vyf spasies ingevoeg soos in MS Word (behalwe herhalende afdelings, herhalende afdelingitems, groepe, vinkbokse, aanhalings). As’n inhoud beheer is gekarteer na persoonlike XML, die verwys XML node is skoongemaak.
Die volgende kode voorbeeld toon hoe om die inhoud van inhoud beheer skoon te maak:
# 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") |
Verander Inhoud Beheer Agtergrond En Grens Kleure
Die StructuredDocumentTag.color eienskap laat jou toe om die kleur van inhoud beheer te kry of stel. Die kleur beïnvloed inhoud beheer in twee situasies:
- MS Word beklemtoon die agtergrond van die inhoudbeheer wanneer die muis oor die inhoudbeheer beweeg. Dit help om die inhoud beheer te identifiseer. Die kleur van die verligting is’n bietjie" sagter " as die color. Byvoorbeeld, MS Word beklemtoon die agtergrond met die pienk kleur, wanneer color rooi is.
- Wanneer jy interaksie (redigering, pluk ens) met die inhoud beheer, is die grens van inhoud beheer gekleur met die color.
Die volgende kode voorbeeld toon hoe om die kleur van inhoud beheer te verander:
# 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") |
Hoe Om Styl Te Stel Om Teks Wat In Die Inhoudsbeheer Getik Is, Te Formateer
As jy die styl van inhoudbeheer wil instel, kan jy StructuredDocumentTag.style of StructuredDocumentTag.style_name eienskappe gebruik. As u die teks in die inhoudsbeheer in die uitvoerdokument tik, sal die getikte teks die styl “Kwotasie"hê.
Die volgende kode voorbeeld toon hoe om die styl van inhoud beheer stel:
# 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") |
Werk Met Herhalende Afdeling Inhoud Beheer
Die herhalende afdeling inhoud beheer laat herhaling van die inhoud wat daarin vervat is. Met behulp van Aspose.Words, kan die gestruktureerde dokument tag nodes van die herhalende afdeling en herhalende afdeling item tipes geskep word en vir hierdie doel, SdtType opsomming tipe bied REPEATING_SECTION_ITEM eiendom.
Die volgende kode voorbeeld toon hoe om’n herhalende gedeelte inhoud beheer te bind aan’n tabel:
# 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") |