//ExEnd public void TestCreate(Document doc) { SmartTag smartTag = (SmartTag)doc.GetChild(NodeType.SmartTag, 0, true); Assert.AreEqual("date", smartTag.Element); Assert.AreEqual("May 29, 2019", smartTag.GetText()); Assert.AreEqual("urn:schemas-microsoft-com:office:smarttags", smartTag.Uri); Assert.AreEqual("Day", smartTag.Properties[0].Name); Assert.AreEqual(string.Empty, smartTag.Properties[0].Uri); Assert.AreEqual("29", smartTag.Properties[0].Value); Assert.AreEqual("Month", smartTag.Properties[1].Name); Assert.AreEqual(string.Empty, smartTag.Properties[1].Uri); Assert.AreEqual("5", smartTag.Properties[1].Value); Assert.AreEqual("Year", smartTag.Properties[2].Name); Assert.AreEqual(string.Empty, smartTag.Properties[2].Uri); Assert.AreEqual("2019", smartTag.Properties[2].Value); smartTag = (SmartTag)doc.GetChild(NodeType.SmartTag, 1, true); Assert.AreEqual("stockticker", smartTag.Element); Assert.AreEqual("MSFT", smartTag.GetText()); Assert.AreEqual("urn:schemas-microsoft-com:office:smarttags", smartTag.Uri); Assert.AreEqual(0, smartTag.Properties.Count); }
[Test] //ExSkip 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"); }
public void AnnotateResult(ReturnType type) { if (type == ReturnType.SmartTag) { string tag = NAMESPACE + "#soylent" + DateTime.Now.Ticks; SmartTag resultTag = Globals.Factory.CreateSmartTag(tag, "Soylent Results: " + text.Text.Substring(0, 10) + "..."); Regex pattern = new Regex(text.Text.Trim().Replace(" ", "\\s"), RegexOptions.IgnorePatternWhitespace); resultTag.Expressions.Add(pattern); List <VSTO.Action> actions = new List <Microsoft.Office.Tools.Word.Action>(); foreach (string result in results) { VSTO.Action action = Globals.Factory.CreateAction(result); action.Click += new ActionClickEventHandler(replaceText); actions.Add(action); } resultTag.Actions = actions.ToArray(); Globals.Soylent.VstoSmartTags.Add(resultTag); } else if (type == ReturnType.Comment) { foreach (string result in results) { object commentText = result; Globals.Soylent.jobToDoc[this.job].Comments.Add(text, ref commentText); foreach (Microsoft.Office.Interop.Word.Comment c in Globals.Soylent.jobToDoc[this.job].Comments) { c.Author = "Turker"; c.Initial = "Turker"; } } } }
protected override void OnIntelliPromptSmartTagClicked(EventArgs e) { ToolStripDropDownClosedEventHandler handler = null; SmartTag tag = base.get_IntelliPrompt().get_SmartTag().get_ActiveSmartTag(); if ((tag != null) && (tag.get_Tag() is NamespaceCompletionData)) { string str; if (base.get_IntelliPrompt().get_MemberList().get_Visible()) { base.get_IntelliPrompt().get_MemberList().Abort(); } base.get_IntelliPrompt().get_SmartTag().set_AutoHideTimeout(0xea60); NamespaceCompletionData data = (NamespaceCompletionData)tag.get_Tag(); ContextMenuStrip strip = new ContextMenuStrip(); foreach (NamespaceSuggestion suggestion in data.Suggestions) { NamespaceSuggestion localItem = suggestion; str = (localItem.ExtraReference == null) ? "" : (" (in " + Path.GetFileName(localItem.ExtraReference) + ")"); strip.Items.Add("using " + localItem.Namespace + str, null, delegate(object sender, EventArgs e) { if (localItem.CaseFixedIdentifier != null) { this.get_Document().ReplaceText(0, new TextRange(data.Offset, data.Offset + data.Length), localItem.CaseFixedIdentifier); } MainForm.Instance.AddNamespaceToQuery(localItem.Namespace); if (localItem.ExtraReference != null) { MainForm.Instance.AddReferenceToQuery(localItem.ExtraReference, true); } this.get_IntelliPrompt().get_SmartTag().Hide(); this.get_IntelliPrompt().get_SmartTag().Clear(); this.AddedNamespace(); }); } strip.Items.Add("-"); foreach (NamespaceSuggestion suggestion in data.Suggestions) { NamespaceSuggestion localItem = suggestion; str = (localItem.ExtraReference == null) ? "" : (" (in " + Path.GetFileName(localItem.ExtraReference) + ")"); strip.Items.Add(localItem.Namespace + "." + (localItem.CaseFixedIdentifier ?? data.Identifier) + str, null, delegate(object sender, EventArgs e) { this.get_Document().ReplaceText(0, new TextRange(data.Offset, data.Offset + data.Length), localItem.Namespace + "." + (localItem.CaseFixedIdentifier ?? data.Identifier)); if (localItem.ExtraReference != null) { MainForm.Instance.AddReferenceToQuery(localItem.ExtraReference, true); } this.AddedNamespace(); }); } if (handler == null) { handler = (sender, e) => base.get_IntelliPrompt().get_SmartTag().set_AutoHideTimeout(0x2710); } strip.Closed += handler; strip.Items[0].Select(); Rectangle rectangle = base.get_IntelliPrompt().get_SmartTag().get_DesktopBounds(); strip.Show(rectangle.X, rectangle.Y + rectangle.Height); } base.OnIntelliPromptSmartTagClicked(e); }
/// <summary> /// Called when a SmartTag node is encountered in the document. /// </summary> public override VisitorAction VisitSmartTagStart(SmartTag smartTag) { IndentAndAppendLine("[SmartTag start] Name: " + smartTag.Element); mDocTraversalDepth++; mVisitorIsInsideSmartTag = true; return(VisitorAction.Continue); }
/// <summary> /// Called when the visiting of a SmartTag node is ended. /// </summary> public override VisitorAction VisitSmartTagEnd(SmartTag smartTag) { mDocTraversalDepth--; IndentAndAppendLine("[SmartTag end]"); mVisitorIsInsideSmartTag = false; return(VisitorAction.Continue); }
public override VisitorAction VisitSmartTagEnd(SmartTag smartTag) { if (IsCompositeAcrossPage(smartTag)) { SplitComposite(smartTag); } return(VisitorAction.Continue); }
[Test] //ExSkip 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.AreEqual(2, doc.GetChildNodes(NodeType.SmartTag, true).Count); doc.RemoveSmartTags(); Assert.AreEqual(0, doc.GetChildNodes(NodeType.SmartTag, true).Count); TestCreate(new Document(ArtifactsDir + "SmartTag.Create.doc")); //ExSkip }
[Test] //ExSkip public void Create() { 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", 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 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()); // SmartTags are supported by older versions of microsoft Word doc.Save(ArtifactsDir + "SmartTag.Create.doc"); // We can strip a document of all its smart tags with RemoveSmartTags() Assert.AreEqual(2, doc.GetChildNodes(NodeType.SmartTag, true).Count); doc.RemoveSmartTags(); Assert.AreEqual(0, doc.GetChildNodes(NodeType.SmartTag, true).Count); TestCreate(new Document(ArtifactsDir + "SmartTag.Create.doc")); //ExSkip }
public void AnnotateResult() { Word.Range text = range; foreach (CrowdproofPatch pp in patches) { string tag = HumanMacroData.NAMESPACE + "#soylent" + DateTime.Now.Ticks; SmartTag resultTag = Globals.Factory.CreateSmartTag(tag, pp.reasons[0]); Regex pattern = new Regex(pp.range.Text.Trim().Replace(" ", "\\s"), RegexOptions.IgnorePatternWhitespace); resultTag.Expressions.Add(pattern); List <VSTO.Action> actions = new List <Microsoft.Office.Tools.Word.Action>(); foreach (string result in pp.replacements) { VSTO.Action action = Globals.Factory.CreateAction(result); action.Click += new ActionClickEventHandler(HumanMacroData.replaceText); actions.Add(action); } /* this doesn't work in Word 2010 */ if (WordVersion.currentVersion < WordVersion.OFFICE_2010) { foreach (string reason in pp.reasons) { VSTO.Action action = Globals.Factory.CreateAction("Error Descriptions///" + reason); action.Click += new ActionClickEventHandler(HumanMacroData.replaceText); actions.Add(action); } } resultTag.Actions = actions.ToArray(); Globals.Soylent.VstoSmartTags.Add(resultTag); if (WordVersion.currentVersion >= WordVersion.OFFICE_2010) { pp.range.Underline = Word.WdUnderline.wdUnderlineWavy; pp.range.Font.UnderlineColor = Word.WdColor.wdColorLavender; } } }
public void SmartTagProperty() { //ExStart //ExFor:CustomXmlProperty //ExFor:CustomXmlProperty.#ctor(String,String,String) //ExFor:CustomXmlProperty.Name //ExFor:CustomXmlProperty.Value //ExSummary:Shows how to work with smart tag properties. 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")); doc.FirstSection.Body.FirstParagraph.AppendChild(smartTag); doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, " is a date.")); doc.Save(ArtifactsDir + "SmartTagProperties.doc"); //ExEnd doc = new Document(ArtifactsDir + "SmartTagProperties.doc"); NodeCollection smartTags = doc.GetChildNodes(NodeType.SmartTag, true); Assert.AreEqual(1, smartTags.Count); smartTag = (SmartTag)smartTags[0]; Assert.AreEqual(3, smartTag.Properties.Count); Assert.AreEqual("29", smartTag.Properties["Day"].Value); Assert.AreEqual("5", smartTag.Properties["Month"].Value); Assert.AreEqual("2019", smartTag.Properties["Year"].Value); }
/// <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); }
public override VisitorAction VisitSmartTagEnd(SmartTag smartTag) { SplitComposite(smartTag); return(VisitorAction.Continue); }
public override VisitorAction VisitSmartTagStart(SmartTag smartTag) { return(ContinueIfCompositeAcrossPageElseSkip(smartTag)); }
/// <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); }
public override VisitorAction VisitSmartTagEnd(SmartTag smartTag) { if (IsCompositeAcrossPage(smartTag)) SplitComposite(smartTag); return VisitorAction.Continue; }
public void SmartTagProperties() { //ExStart //ExFor:CustomXmlProperty.Uri //ExFor:CustomXmlPropertyCollection //ExFor:CustomXmlPropertyCollection.Add(CustomXmlProperty) //ExFor:CustomXmlPropertyCollection.Clear //ExFor:CustomXmlPropertyCollection.Contains(String) //ExFor:CustomXmlPropertyCollection.Count //ExFor:CustomXmlPropertyCollection.GetEnumerator //ExFor:CustomXmlPropertyCollection.IndexOfKey(String) //ExFor:CustomXmlPropertyCollection.Item(Int32) //ExFor:CustomXmlPropertyCollection.Item(String) //ExFor:CustomXmlPropertyCollection.Remove(String) //ExFor:CustomXmlPropertyCollection.RemoveAt(Int32) //ExSummary:Shows how to work with smart tag properties to get in depth information about smart tags. // Open a document that contains smart tags and their collection Document doc = new Document(MyDir + "SmartTags.doc"); // Smart tags are an older Microsoft Word feature that can automatically detect and tag // any parts of the text that it registers as commonly used information objects such as names, addresses, stock tickers, dates etc // In Word 2003, smart tags can be turned on in Tools > AutoCorrect options... > SmartTags tab // In our input document there are three objects that were registered as smart tags, but since they can be nested, we have 8 in this collection NodeCollection smartTags = doc.GetChildNodes(NodeType.SmartTag, true); Assert.AreEqual(8, smartTags.Count); // The last smart tag is of the "Date" type, which we will retrieve here SmartTag smartTag = (SmartTag)smartTags[7]; // The Properties attribute, for some smart tags, elaborates on the text object that Word picked up as a smart tag // In the case of our "Date" smart tag, its properties will let us know the year, month and day within the smart tag CustomXmlPropertyCollection properties = smartTag.Properties; // We can enumerate over the collection and print the aforementioned properties to the console Assert.AreEqual(4, properties.Count); using (IEnumerator <CustomXmlProperty> enumerator = properties.GetEnumerator()) { while (enumerator.MoveNext()) { Console.WriteLine($"Property name: {enumerator.Current.Name}, value: {enumerator.Current.Value}"); Assert.AreEqual("", enumerator.Current.Uri); } } // We can also access the elements in various ways, including as a key-value pair Assert.True(properties.Contains("Day")); Assert.AreEqual("22", properties["Day"].Value); Assert.AreEqual("2003", properties[2].Value); Assert.AreEqual(1, properties.IndexOfKey("Month")); // We can also remove elements by name, index or clear the collection entirely properties.RemoveAt(3); properties.Remove("Year"); Assert.AreEqual(2, (properties.Count)); properties.Clear(); Assert.AreEqual(0, (properties.Count)); // We can remove the entire smart tag like this smartTag.Remove(); //ExEnd }