SmartTag
SmartTag class
This element specifies the presence of a smart tag around one or more inline structures (runs, images, fields,etc.) within a paragraph.
To learn more, visit the Structured Document Tags or Content Control documentation article.
public class SmartTag : CompositeNode
Constructors
| Name | Description |
|---|---|
| SmartTag(DocumentBase) | Initializes a new instance of the SmartTag class. |
Properties
| Name | Description |
|---|---|
| Count { get; } | Gets the number of immediate children of this node. |
| CustomNodeId { get; set; } | Specifies custom node identifier. |
| virtual Document { get; } | Gets the document to which this node belongs. |
| Element { get; set; } | Specifies the name of the smart tag within the document. |
| FirstChild { get; } | Gets the first child of the node. |
| HasChildNodes { get; } | Returns true if this node has any child nodes. |
| override IsComposite { get; } | Returns true as this node can have child nodes. |
| LastChild { get; } | Gets the last child of the node. |
| NextSibling { get; } | Gets the node immediately following this node. |
| override NodeType { get; } | Returns SmartTag. |
| ParentNode { get; } | Gets the immediate parent of this node. |
| PreviousSibling { get; } | Gets the node immediately preceding this node. |
| Properties { get; } | A collection of the smart tag properties. |
| Range { get; } | Returns a Range object that represents the portion of a document that is contained in this node. |
| Uri { get; set; } | Specifies the namespace URI of the smart tag. |
Methods
| Name | Description |
|---|---|
| override Accept(DocumentVisitor) | Accepts a visitor. |
| override AcceptEnd(DocumentVisitor) | Accepts a visitor for visiting the end of the SmartTag. |
| override AcceptStart(DocumentVisitor) | Accepts a visitor for visiting the start of the SmartTag. |
| AppendChild<T>(T) | Adds the specified node to the end of the list of child nodes for this node. |
| Clone(bool) | Creates a duplicate of the node. |
| CreateNavigator() | Creates navigator which can be used to traverse and read nodes. |
| GetAncestor(NodeType) | Gets the first ancestor of the specified NodeType. |
| GetAncestor(Type) | Gets the first ancestor of the specified object type. |
| GetChild(NodeType, int, bool) | Returns an Nth child node that matches the specified type. |
| GetChildNodes(NodeType, bool) | Returns a live collection of child nodes that match the specified type. |
| GetEnumerator() | Provides support for the for each style iteration over the child nodes of this node. |
| override GetText() | Gets the text of this node and of all its children. |
| IndexOf(Node) | Returns the index of the specified child node in the child node array. |
| InsertAfter<T>(T, Node) | Inserts the specified node immediately after the specified reference node. |
| InsertBefore<T>(T, Node) | Inserts the specified node immediately before the specified reference node. |
| NextPreOrder(Node) | Gets next node according to the pre-order tree traversal algorithm. |
| PrependChild<T>(T) | Adds the specified node to the beginning of the list of child nodes for this node. |
| PreviousPreOrder(Node) | Gets the previous node according to the pre-order tree traversal algorithm. |
| Remove() | Removes itself from the parent. |
| RemoveAllChildren() | Removes all the child nodes of the current node. |
| RemoveChild<T>(T) | Removes the specified child node. |
| RemoveSmartTags() | Removes all SmartTag descendant nodes of the current node. |
| SelectNodes(string) | Selects a list of nodes matching the XPath expression. |
| SelectSingleNode(string) | Selects the first Node that matches the XPath expression. |
| ToString(SaveFormat) | Exports the content of the node into a string in the specified format. |
| ToString(SaveOptions) | Exports the content of the node into a string using the specified save options. |
Remarks
Smart tags is a kind of custom XML markup. Smart tags provide a facility for embedding customer-defined semantics into the document via the ability to provide a basic namespace/name for a run or set of runs within a document.
SmartTag can be a child of a Paragraph or another SmartTag node.
The complete list of child nodes that can occur inside a smart tag consists of BookmarkStart, BookmarkEnd, FieldStart, FieldSeparator, FieldEnd, FormField, Comment, Footnote, Run, SpecialChar, Shape, GroupShape, CommentRangeStart, CommentRangeEnd, SmartTag.
Examples
Shows how to create smart tags.
public void Create()
{
Document doc = new Document();
// A smart tag appears in a document with Microsoft Word recognizes a part of its text as some form of data,
// such as a name, date, or address, and converts it to a hyperlink that displays a purple dotted underline.
SmartTag smartTag = new SmartTag(doc);
// Smart tags are composite nodes that contain their recognized text in its entirety.
// Add contents to this smart tag manually.
smartTag.AppendChild(new Run(doc, "May 29, 2019"));
// Microsoft Word may recognize the above contents as being a date.
// Smart tags use the "Element" property to reflect the type of data they contain.
smartTag.Element = "date";
// Some smart tag types process their contents further into custom XML properties.
smartTag.Properties.Add(new CustomXmlProperty("Day", string.Empty, "29"));
smartTag.Properties.Add(new CustomXmlProperty("Month", string.Empty, "5"));
smartTag.Properties.Add(new CustomXmlProperty("Year", string.Empty, "2019"));
// Set the smart tag's URI to the default value.
smartTag.Uri = "urn:schemas-microsoft-com:office:smarttags";
doc.FirstSection.Body.FirstParagraph.AppendChild(smartTag);
doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, " is a date. "));
// Create another smart tag for a stock ticker.
smartTag = new SmartTag(doc);
smartTag.Element = "stockticker";
smartTag.Uri = "urn:schemas-microsoft-com:office:smarttags";
smartTag.AppendChild(new Run(doc, "MSFT"));
doc.FirstSection.Body.FirstParagraph.AppendChild(smartTag);
doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, " is a stock ticker."));
// Print all the smart tags in our document using a document visitor.
doc.Accept(new SmartTagPrinter());
// Older versions of Microsoft Word support smart tags.
doc.Save(ArtifactsDir + "SmartTag.Create.doc");
// Use the "RemoveSmartTags" method to remove all smart tags from a document.
Assert.That(doc.GetChildNodes(NodeType.SmartTag, true).Count, Is.EqualTo(2));
doc.RemoveSmartTags();
Assert.That(doc.GetChildNodes(NodeType.SmartTag, true).Count, Is.EqualTo(0));
}
/// <summary>
/// Prints visited smart tags and their contents.
/// </summary>
private class SmartTagPrinter : DocumentVisitor
{
/// <summary>
/// Called when a SmartTag node is encountered in the document.
/// </summary>
public override VisitorAction VisitSmartTagStart(SmartTag smartTag)
{
Console.WriteLine($"Smart tag type: {smartTag.Element}");
return VisitorAction.Continue;
}
/// <summary>
/// Called when the visiting of a SmartTag node is ended.
/// </summary>
public override VisitorAction VisitSmartTagEnd(SmartTag smartTag)
{
Console.WriteLine($"\tContents: \"{smartTag.ToString(SaveFormat.Text)}\"");
if (smartTag.Properties.Count == 0)
{
Console.WriteLine("\tContains no properties");
}
else
{
Console.Write("\tProperties: ");
string[] properties = new string[smartTag.Properties.Count];
int index = 0;
foreach (CustomXmlProperty cxp in smartTag.Properties)
properties[index++] = $"\"{cxp.Name}\" = \"{cxp.Value}\"";
Console.WriteLine(string.Join(", ", properties));
}
return VisitorAction.Continue;
}
}
See Also
- class CompositeNode
- namespace Aspose.Words.Markup
- assembly Aspose.Words