SmartTag Constructor

Initializes a new instance of the SmartTag class.

Namespace:  Aspose.Words.Markup
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public SmartTag(
	DocumentBase doc
)

Parameters

doc
Type: Aspose.WordsDocumentBase
The owner document.
Remarks

When you create a new node, you need to specify a document to which the node belongs. A node cannot exist without a document because it depends on the document-wide structures such as lists and styles. Although a node always belongs to a document, a node might or might not be a part of the document tree.

When a node is created, it belongs to a document, but is not yet part of the document tree and ParentNode is null. To insert a node into the document, use the InsertAfter(Node, Node) or InsertBefore(Node, Node) methods on the parent node.

Examples
Shows how to create smart tags.
public void SmartTags()
{
    Document doc = new Document();
    SmartTag smartTag = new SmartTag(doc);
    smartTag.Element = "date";

    // Specify a date and set smart tag properties accordingly
    smartTag.AppendChild(new Run(doc, "May 29, 2019"));

    smartTag.Properties.Add(new CustomXmlProperty("Day", "", "29"));
    smartTag.Properties.Add(new CustomXmlProperty("Month", "", "5"));
    smartTag.Properties.Add(new CustomXmlProperty("Year", "", "2019"));

    // Set the smart tag's uri to the default
    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 and add one more smart tag, this time for a financial symbol
    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 with a document visitor
    doc.Accept(new SmartTagVisitor());

    doc.Save(ArtifactsDir + "StructuredDocumentTag.SmartTags.docx");
}

/// <summary>
/// DocumentVisitor implementation that prints smart tags and their contents
/// </summary>
private class SmartTagVisitor : 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