SmartTag
内容
[
隐藏
]SmartTag class
此元素指定段落内一个或多个内联结构(运行、图像、字段等)周围是否存在智能标记。
要了解更多信息,请访问结构化文档标签或内容控制文档文章。
public class SmartTag : CompositeNode
构造函数
| 姓名 | 描述 |
|---|---|
| SmartTag(DocumentBase) | 初始化SmartTag类. |
特性
| 姓名 | 描述 |
|---|---|
| Count { get; } | 获取此节点的直属子节点的数量。 |
| CustomNodeId { get; set; } | 指定自定义节点标识符。 |
| virtual Document { get; } | 获取此节点所属的文档。 |
| Element { get; set; } | 指定文档中的智能标记的名称。 |
| FirstChild { get; } | 获取节点的第一个子节点。 |
| HasChildNodes { get; } | 返回真的如果此节点有任何子节点。 |
| override IsComposite { get; } | 返回真的因为这个节点可以有子节点。 |
| LastChild { get; } | 获取节点的最后一个子节点。 |
| NextSibling { get; } | 获取紧随此节点之后的节点。 |
| override NodeType { get; } | 返回SmartTag. |
| ParentNode { get; } | 获取此节点的直接父节点。 |
| PreviousSibling { get; } | 获取此节点前一个节点。 |
| Properties { get; } | 智能标签属性的集合。 |
| Range { get; } | 返回Range表示此节点中包含的文档部分的对象。 |
| Uri { get; set; } | 指定智能标记的命名空间 URI。 |
方法
| 姓名 | 描述 |
|---|---|
| override Accept(DocumentVisitor) | 接受访客。 |
| override AcceptEnd(DocumentVisitor) | 接受访客访问智能标签的末尾。 |
| override AcceptStart(DocumentVisitor) | 接受访客访问智能标签的开头。 |
| AppendChild<T>(T) | 将指定节点添加到此节点的子节点列表的末尾。 |
| Clone(bool) | 创建节点的副本。 |
| CreateNavigator() | 创建可用于遍历和读取节点的导航器。 |
| GetAncestor(NodeType) | 获取指定的第一个祖先NodeType. |
| GetAncestor(Type) | 获取指定对象类型的第一个祖先。 |
| GetChild(NodeType, int, bool) | 返回与指定类型匹配的第 N 个子节点。 |
| GetChildNodes(NodeType, bool) | 返回与指定类型匹配的子节点的实时集合。 |
| GetEnumerator() | 为该节点的子节点提供对每个样式迭代的支持。 |
| override GetText() | 获取此节点及其所有子节点的文本。 |
| IndexOf(Node) | 返回子节点数组中指定子节点的索引。 |
| InsertAfter<T>(T, Node) | 在指定的参考节点后立即插入指定的节点。 |
| InsertBefore<T>(T, Node) | 在指定的参考节点之前立即插入指定的节点。 |
| NextPreOrder(Node) | 根据前序树遍历算法获取下一个节点。 |
| PrependChild<T>(T) | 将指定节点添加到此节点的子节点列表的开头。 |
| PreviousPreOrder(Node) | 根据前序树遍历算法获取前一个节点。 |
| Remove() | 将自身从父级中移除。 |
| RemoveAllChildren() | 删除当前节点的所有子节点。 |
| RemoveChild<T>(T) | 删除指定的子节点。 |
| RemoveSmartTags() | 删除所有SmartTag当前节点的后代节点。 |
| SelectNodes(string) | 选择与 XPath 表达式匹配的节点列表。 |
| SelectSingleNode(string) | 选择第一个Node与 XPath 表达式匹配。 |
| ToString(SaveFormat) | 将节点的内容导出为指定格式的字符串。 |
| ToString(SaveOptions) | 使用指定的保存选项将节点内容导出为字符串。 |
评论
智能标签是一种自定义 XML 标记。智能标签能够为文档中的一行或多行代码提供基本的命名空间/名称,从而将客户定义的语义嵌入到文档中。
SmartTag可以是Paragraphor 另一个SmartTag节点。
智能标记内可出现的子节点的完整列表包括 BookmarkStart,BookmarkEnd, FieldStart,FieldSeparator,FieldEnd,FormField, Comment,Footnote, Run,SpecialChar, Shape,GroupShape, CommentRangeStart, CommentRangeEnd, SmartTag。
例子
展示如何创建智能标签。
public void Create()
{
Document doc = new Document();
// 智能标签出现在 Microsoft Word 的文档中,它将其部分文本识别为某种形式的数据,
// 例如姓名、日期或地址,并将其转换为显示紫色虚线下划线的超链接。
SmartTag smartTag = new SmartTag(doc);
// 智能标签是包含其识别的全部文本的复合节点。
// 手动将内容添加到此智能标签。
smartTag.AppendChild(new Run(doc, "May 29, 2019"));
// Microsoft Word 可能会将上述内容识别为日期。
// 智能标签使用“元素”属性来反映它们包含的数据类型。
smartTag.Element = "date";
// 一些智能标记类型将其内容进一步处理为自定义 XML 属性。
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"));
// 将智能标记的 URI 设置为默认值。
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. "));
// 为股票行情自动收录器创建另一个智能标签。
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."));
// 使用文档访问器打印文档中的所有智能标签。
doc.Accept(new SmartTagPrinter());
// 旧版本的 Microsoft Word 支持智能标签。
doc.Save(ArtifactsDir + "SmartTag.Create.doc");
// 使用“RemoveSmartTags”方法从文档中删除所有智能标签。
Assert.AreEqual(2, doc.GetChildNodes(NodeType.SmartTag, true).Count);
doc.RemoveSmartTags();
Assert.AreEqual(0, doc.GetChildNodes(NodeType.SmartTag, true).Count);
}
/// <summary>
/// 打印访问过的智能标签及其内容。
/// </summary>
private class SmartTagPrinter : DocumentVisitor
{
/// <summary>
/// 当在文档中遇到 SmartTag 节点时调用。
/// </summary>
public override VisitorAction VisitSmartTagStart(SmartTag smartTag)
{
Console.WriteLine($"Smart tag type: {smartTag.Element}");
return VisitorAction.Continue;
}
/// <summary>
/// 当对 SmartTag 节点的访问结束时调用。
/// </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;
}
}
也可以看看
- class CompositeNode
- 命名空间 Aspose.Words.Markup
- 部件 Aspose.Words