Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
In Microsoft Word, you can create a form by starting with a template and adding content controls, including checkboxes, text boxes, date pickers, and drop-down lists. In Aspose.Words, a Structured Document Tag or content control from any document loaded into Aspose.Words is imported as a StructuredDocumentTag node. Structured document tags (SDT or content control) allow embedding customer-defined semantics as well as its behaviour and appearance into a document.
StructuredDocumentTag can occur in a document in the following places:
In this version of Aspose.Words, the following types of SDT or content control can be created:
Picture
The following code example shows how to create content control of type checkbox:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline); | |
builder.InsertNode(sdtCheckBox); | |
doc.Save(ArtifactsDir + "WorkingWithSdt.SdtCheckBox.docx", SaveFormat.Docx); |
The following code example shows how to create content control of type rich text box:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block); | |
Paragraph para = new Paragraph(doc); | |
Run run = new Run(doc); | |
run.Text = "Hello World"; | |
run.Font.Color = Color.Green; | |
para.Runs.Add(run); | |
sdtRichText.GetChildNodes(NodeType.Any, false).Add(para); | |
doc.FirstSection.Body.AppendChild(sdtRichText); | |
doc.Save(ArtifactsDir + "WorkingWithSdt.SdtRichTextBox.docx"); |
The following code example shows how to create content control of the type combo box:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.ComboBox, MarkupLevel.Block); | |
sdt.ListItems.Add(new SdtListItem("Choose an item", "-1")); | |
sdt.ListItems.Add(new SdtListItem("Item 1", "1")); | |
sdt.ListItems.Add(new SdtListItem("Item 2", "2")); | |
doc.FirstSection.Body.AppendChild(sdt); | |
doc.Save(ArtifactsDir + "WorkingWithSdt.SdtComboBox.docx"); |
This section explains how to update the values of SDT or content control programmatically.
The following code example shows how to set the current state of the checkbox:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Structured document tags.docx"); | |
// Get the first content control from the document. | |
StructuredDocumentTag sdtCheckBox = | |
(StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true); | |
if (sdtCheckBox.SdtType == SdtType.Checkbox) | |
sdtCheckBox.Checked = true; | |
doc.Save(ArtifactsDir + "WorkingWithSdt.CurrentStateOfCheckBox.docx"); |
The following code example shows how to modify content controls of type plain text box, drop-down list and picture:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Structured document tags.docx"); | |
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true)) | |
{ | |
switch (sdt.SdtType) | |
{ | |
case SdtType.PlainText: | |
{ | |
sdt.RemoveAllChildren(); | |
Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph; | |
Run run = new Run(doc, "new text goes here"); | |
para.AppendChild(run); | |
break; | |
} | |
case SdtType.DropDownList: | |
{ | |
SdtListItem secondItem = sdt.ListItems[2]; | |
sdt.ListItems.SelectedValue = secondItem; | |
break; | |
} | |
case SdtType.Picture: | |
{ | |
Shape shape = (Shape) sdt.GetChild(NodeType.Shape, 0, true); | |
if (shape.HasImage) | |
{ | |
shape.ImageData.SetImage(ImagesDir + "Watermark.png"); | |
} | |
break; | |
} | |
} | |
} | |
doc.Save(ArtifactsDir + "WorkingWithSdt.ModifySdt.docx"); |
You can bind content controls with XML data (custom XML part) in Word documents.
The following code example shows how to bind content control to custom XML parts:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
CustomXmlPart xmlPart = | |
doc.CustomXmlParts.Add(Guid.NewGuid().ToString("B"), "<root><text>Hello, World!</text></root>"); | |
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block); | |
doc.FirstSection.Body.AppendChild(sdt); | |
sdt.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", ""); | |
doc.Save(ArtifactsDir + "WorkingWithSdt.BindSdtToCustomXmlPart.doc"); |
You can get the mapping of this structured document tag range to XML data in a custom XML part of the current document using the StructuredDocumentTagRangeStart.XmlMapping property. However, the SetMapping method can be used to map a structured document tag range to XML data.
The following code example shows how to set XML mapping:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Multi-section structured document tags.docx"); | |
// Construct an XML part that contains data and add it to the document's CustomXmlPart collection. | |
string xmlPartId = Guid.NewGuid().ToString("B"); | |
string xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>"; | |
CustomXmlPart xmlPart = doc.CustomXmlParts.Add(xmlPartId, xmlPartContent); | |
Console.WriteLine(Encoding.UTF8.GetString(xmlPart.Data)); | |
// Create a StructuredDocumentTag that will display the contents of our CustomXmlPart in the document. | |
StructuredDocumentTagRangeStart sdtRangeStart = (StructuredDocumentTagRangeStart)doc.GetChild(NodeType.StructuredDocumentTagRangeStart, 0, true); | |
// 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.XmlMapping.SetMapping(xmlPart, "/root[1]/text[2]", null); | |
doc.Save(ArtifactsDir + "WorkingWithSdt.SdtRangeStartXmlMapping.docx"); |
You can clear the contents of a content control with displaying a placeholder. The StructuredDocumentTag.Clear method clears contents of this structured document tag and displays a placeholder if it is defined. However, It is not possible to clear the contents of a content control if it has revisions. If a content control has no placeholder, five spaces are inserted like in Microsoft Word (except repeating sections, repeating section items, groups, check-boxes, citations). If a content control is mapped to custom XML, the referenced XML node is cleared.
The following code example shows how to clear the content of content control:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Structured document tags.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true); | |
sdt.Clear(); | |
doc.Save(ArtifactsDir + "WorkingWithSdt.ClearSdt.doc"); |
The StructuredDocumentTag.Color
property allows you to get or set the color of content control. The color affects content control in two situations:
The following code example shows how to change the color of content control:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Structured document tags.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true); | |
sdt.Color = Color.Red; | |
doc.Save(ArtifactsDir + "WorkingWithSdt.SdtColor.docx"); |
If you want to set the style of content control, you can use StructuredDocumentTag.Style
or StructuredDocumentTag.StyleName
properties. When you type the text into content control in the output document, the typed text will have the style “Quote”.
The following code example shows how to set the style of content control:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Structured document tags.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true); | |
Style style = doc.Styles[StyleIdentifier.Quote]; | |
sdt.Style = style; | |
doc.Save(ArtifactsDir + "WorkingWithSdt.SdtStyle.docx"); |
The repeating section content control allows repeating the content contained within it. Using Aspose.Words, the structured document tag nodes of the repeating section and repeating section item types can be created and for this purpose, SdtType enumeration type provides RepeatingSectionItem property.
The following code example shows how to bind a repeating section content control to a table.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
CustomXmlPart xmlPart = doc.CustomXmlParts.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 table = builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Title"); | |
builder.InsertCell(); | |
builder.Write("Author"); | |
builder.EndRow(); | |
builder.EndTable(); | |
StructuredDocumentTag repeatingSectionSdt = | |
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row); | |
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", ""); | |
table.AppendChild(repeatingSectionSdt); | |
StructuredDocumentTag repeatingSectionItemSdt = | |
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row); | |
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt); | |
Row row = new Row(doc); | |
repeatingSectionItemSdt.AppendChild(row); | |
StructuredDocumentTag titleSdt = | |
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); | |
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", ""); | |
row.AppendChild(titleSdt); | |
StructuredDocumentTag authorSdt = | |
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell); | |
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", ""); | |
row.AppendChild(authorSdt); | |
doc.Save(ArtifactsDir + "WorkingWithSdt.RepeatingSectionMappedToCustomXmlPart.docx"); |
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.