public void ClearTextFromStructuredDocumentTags() { //ExStart //ExFor:StructuredDocumentTag.Clear //ExSummary:Shows how to delete contents of structured document tag elements. Document doc = new Document(); // Create a plain text structured document tag, and then append it to the document. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block); doc.FirstSection.Body.AppendChild(tag); // This structured document tag, which is in the form of a text box, already displays placeholder text. Assert.AreEqual("Click here to enter text.", tag.GetText().Trim()); Assert.True(tag.IsShowingPlaceholderText); // Create a building block with text contents. GlossaryDocument glossaryDoc = doc.GlossaryDocument; BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc); substituteBlock.Name = "My placeholder"; substituteBlock.AppendChild(new Section(glossaryDoc)); substituteBlock.FirstSection.EnsureMinimum(); substituteBlock.FirstSection.Body.FirstParagraph.AppendChild(new Run(glossaryDoc, "Custom placeholder text.")); glossaryDoc.AppendChild(substituteBlock); // Set the structured document tag's "PlaceholderName" property to our building block's name to get // the structured document tag to display the contents of the building block in place of the original default text. tag.PlaceholderName = "My placeholder"; Assert.AreEqual("Custom placeholder text.", tag.GetText().Trim()); Assert.True(tag.IsShowingPlaceholderText); // Edit the text of the structured document tag and hide the placeholder text. Run run = (Run)tag.GetChild(NodeType.Run, 0, true); run.Text = "New text."; tag.IsShowingPlaceholderText = false; Assert.AreEqual("New text.", tag.GetText().Trim()); // Use the "Clear" method to clear this structured document tag's contents and display the placeholder again. tag.Clear(); Assert.True(tag.IsShowingPlaceholderText); Assert.AreEqual("Custom placeholder text.", tag.GetText().Trim()); //ExEnd }
public void ClearTextFromStructuredDocumentTags() { //ExStart //ExFor:StructuredDocumentTag.Clear //ExSummary:Shows how to delete content of StructuredDocumentTag elements. Document doc = new Document(); // Create a plain text structured document tag and append it to the document StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block); doc.FirstSection.Body.AppendChild(tag); // This structured document tag, which is in the form of a text box, already displays placeholder text Assert.AreEqual("Click here to enter text.", tag.GetText().Trim()); Assert.True(tag.IsShowingPlaceholderText); // Create a building block that GlossaryDocument glossaryDoc = doc.GlossaryDocument; BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc); substituteBlock.Name = "My placeholder"; substituteBlock.AppendChild(new Section(glossaryDoc)); substituteBlock.FirstSection.EnsureMinimum(); substituteBlock.FirstSection.Body.FirstParagraph.AppendChild(new Run(glossaryDoc, "Custom placeholder text.")); glossaryDoc.AppendChild(substituteBlock); // Set the tag's placeholder to the building block tag.PlaceholderName = "My placeholder"; Assert.AreEqual("Custom placeholder text.", tag.GetText().Trim()); Assert.True(tag.IsShowingPlaceholderText); // Edit the text of the structured document tag and disable showing of placeholder text Run run = (Run)tag.GetChild(NodeType.Run, 0, true); run.Text = "New text."; tag.IsShowingPlaceholderText = false; Assert.AreEqual("New text.", tag.GetText().Trim()); tag.Clear(); // Clearing a PlainText tag reverts these changes Assert.True(tag.IsShowingPlaceholderText); Assert.AreEqual("Custom placeholder text.", tag.GetText().Trim()); //ExEnd }
public void XmlMapping() { //ExStart //ExFor:XmlMapping //ExFor:XmlMapping.CustomXmlPart //ExFor:XmlMapping.Delete //ExFor:XmlMapping.IsMapped //ExFor:XmlMapping.PrefixMappings //ExFor:XmlMapping.XPath //ExSummary:Shows how to set XML mappings for custom XML parts. Document doc = new Document(); // Construct an XML part that contains text 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); Assert.AreEqual("<root><text>Text element #1</text><text>Text element #2</text></root>", Encoding.UTF8.GetString(xmlPart.Data)); // Create a structured document tag that will display the contents of our CustomXmlPart. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block); // Set a mapping for our structured document tag. This mapping will instruct // our structured document tag to display a portion of the XML part's text contents that the XPath points to. // In this case, it will be contents of the the second "<text>" element of the first "<root>" element: "Text element #2". tag.XmlMapping.SetMapping(xmlPart, "/root[1]/text[2]", "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); Assert.True(tag.XmlMapping.IsMapped); Assert.AreEqual(xmlPart, tag.XmlMapping.CustomXmlPart); Assert.AreEqual("/root[1]/text[2]", tag.XmlMapping.XPath); Assert.AreEqual("xmlns:ns='http://www.w3.org/2001/XMLSchema'", tag.XmlMapping.PrefixMappings); // Add the structured document tag to the document to display the content from our custom part. doc.FirstSection.Body.AppendChild(tag); doc.Save(ArtifactsDir + "StructuredDocumentTag.XmlMapping.docx"); //ExEnd doc = new Document(ArtifactsDir + "StructuredDocumentTag.XmlMapping.docx"); xmlPart = doc.CustomXmlParts[0]; Assert.True(Guid.TryParse(xmlPart.Id, out Guid temp)); Assert.AreEqual("<root><text>Text element #1</text><text>Text element #2</text></root>", Encoding.UTF8.GetString(xmlPart.Data)); tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true); Assert.AreEqual("Text element #2", tag.GetText().Trim()); Assert.AreEqual("/root[1]/text[2]", tag.XmlMapping.XPath); Assert.AreEqual("xmlns:ns='http://www.w3.org/2001/XMLSchema'", tag.XmlMapping.PrefixMappings); }
public void CreatingCustomXml() { //ExStart //ExFor:CustomXmlPart //ExFor:CustomXmlPart.Clone //ExFor:CustomXmlPart.Data //ExFor:CustomXmlPart.Id //ExFor:CustomXmlPart.Schemas //ExFor:CustomXmlPartCollection //ExFor:CustomXmlPartCollection.Add(CustomXmlPart) //ExFor:CustomXmlPartCollection.Add(String, String) //ExFor:CustomXmlPartCollection.Clear //ExFor:CustomXmlPartCollection.Clone //ExFor:CustomXmlPartCollection.Count //ExFor:CustomXmlPartCollection.GetById(String) //ExFor:CustomXmlPartCollection.GetEnumerator //ExFor:CustomXmlPartCollection.Item(Int32) //ExFor:CustomXmlPartCollection.RemoveAt(Int32) //ExFor:Document.CustomXmlParts //ExFor:StructuredDocumentTag.XmlMapping //ExFor:XmlMapping.SetMapping(CustomXmlPart, String, String) //ExSummary:Shows how to create structured document tag with a custom XML data. Document doc = new Document(); // Construct an XML part that contains data and add it to the document's collection // Once the "Developer" tab in Microsoft Word is enabled, // we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane" string xmlPartId = Guid.NewGuid().ToString("B"); string xmlPartContent = "<root><text>Hello world!</text></root>"; CustomXmlPart xmlPart = doc.CustomXmlParts.Add(xmlPartId, xmlPartContent); // The data we entered is stored in these attributes Assert.AreEqual(Encoding.ASCII.GetBytes(xmlPartContent), xmlPart.Data); Assert.AreEqual(xmlPartId, xmlPart.Id); // XML parts can be referenced by collection index or GUID Assert.AreEqual(xmlPart, doc.CustomXmlParts[0]); Assert.AreEqual(xmlPart, doc.CustomXmlParts.GetById(xmlPartId)); // Once the part is created, we can add XML schema associations like this xmlPart.Schemas.Add("http://www.w3.org/2001/XMLSchema"); // We can also clone parts and insert them into the collection directly CustomXmlPart xmlPartClone = xmlPart.Clone(); xmlPartClone.Id = Guid.NewGuid().ToString("B"); doc.CustomXmlParts.Add(xmlPartClone); Assert.AreEqual(2, doc.CustomXmlParts.Count); // Iterate through collection with an enumerator and print the contents of each part using (IEnumerator <CustomXmlPart> enumerator = doc.CustomXmlParts.GetEnumerator()) { int index = 0; while (enumerator.MoveNext()) { Console.WriteLine($"XML part index {index}, ID: {enumerator.Current.Id}"); Console.WriteLine($"\tContent: {Encoding.UTF8.GetString(enumerator.Current.Data)}"); index++; } } // XML parts can be removed by index doc.CustomXmlParts.RemoveAt(1); Assert.AreEqual(1, doc.CustomXmlParts.Count); // The XML part collection itself can be cloned also CustomXmlPartCollection customXmlParts = doc.CustomXmlParts.Clone(); // And all elements can be cleared like this customXmlParts.Clear(); // Create a StructuredDocumentTag that will display the contents of our part, // insert it into the document and save the document StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block); tag.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", string.Empty); doc.FirstSection.Body.AppendChild(tag); doc.Save(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx"); //ExEnd Assert.IsTrue(DocumentHelper.CompareDocs(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx", GoldsDir + "StructuredDocumentTag.CustomXml Gold.docx")); doc = new Document(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx"); xmlPart = doc.CustomXmlParts[0]; Assert.True(Guid.TryParse(xmlPart.Id, out Guid temp)); Assert.AreEqual("<root><text>Hello world!</text></root>", Encoding.UTF8.GetString(xmlPart.Data)); Assert.AreEqual("http://www.w3.org/2001/XMLSchema", xmlPart.Schemas[0]); tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true); Assert.AreEqual("Hello world!", tag.GetText().Trim()); Assert.AreEqual("/root[1]/text[1]", tag.XmlMapping.XPath); Assert.AreEqual(string.Empty, tag.XmlMapping.PrefixMappings); }
public void CreatingCustomXml() { //ExStart //ExFor:CustomXmlPart //ExFor:CustomXmlPart.Clone //ExFor:CustomXmlPart.Data //ExFor:CustomXmlPart.Id //ExFor:CustomXmlPart.Schemas //ExFor:CustomXmlPartCollection //ExFor:CustomXmlPartCollection.Add(CustomXmlPart) //ExFor:CustomXmlPartCollection.Add(String, String) //ExFor:CustomXmlPartCollection.Clear //ExFor:CustomXmlPartCollection.Clone //ExFor:CustomXmlPartCollection.Count //ExFor:CustomXmlPartCollection.GetById(String) //ExFor:CustomXmlPartCollection.GetEnumerator //ExFor:CustomXmlPartCollection.Item(Int32) //ExFor:CustomXmlPartCollection.RemoveAt(Int32) //ExFor:Document.CustomXmlParts //ExFor:StructuredDocumentTag.XmlMapping //ExFor:XmlMapping.SetMapping(CustomXmlPart, String, String) //ExSummary:Shows how to create a structured document tag with custom XML data. Document doc = new Document(); // Construct an XML part that contains data and add it to the document's collection. // If we enable the "Developer" tab in Microsoft Word, // we can find elements from this collection in the "XML Mapping Pane", along with a few default elements. string xmlPartId = Guid.NewGuid().ToString("B"); string xmlPartContent = "<root><text>Hello world!</text></root>"; CustomXmlPart xmlPart = doc.CustomXmlParts.Add(xmlPartId, xmlPartContent); Assert.AreEqual(Encoding.ASCII.GetBytes(xmlPartContent), xmlPart.Data); Assert.AreEqual(xmlPartId, xmlPart.Id); // Below are two ways to refer to XML parts. // 1 - By an index in the custom XML part collection: Assert.AreEqual(xmlPart, doc.CustomXmlParts[0]); // 2 - By GUID: Assert.AreEqual(xmlPart, doc.CustomXmlParts.GetById(xmlPartId)); // Add an XML schema association. xmlPart.Schemas.Add("http://www.w3.org/2001/XMLSchema"); // Clone a part, and then insert it into the collection. CustomXmlPart xmlPartClone = xmlPart.Clone(); xmlPartClone.Id = Guid.NewGuid().ToString("B"); doc.CustomXmlParts.Add(xmlPartClone); Assert.AreEqual(2, doc.CustomXmlParts.Count); // Iterate through the collection and print the contents of each part. using (IEnumerator <CustomXmlPart> enumerator = doc.CustomXmlParts.GetEnumerator()) { int index = 0; while (enumerator.MoveNext()) { Console.WriteLine($"XML part index {index}, ID: {enumerator.Current.Id}"); Console.WriteLine($"\tContent: {Encoding.UTF8.GetString(enumerator.Current.Data)}"); index++; } } // Use the "RemoveAt" method to remove the cloned part by index. doc.CustomXmlParts.RemoveAt(1); Assert.AreEqual(1, doc.CustomXmlParts.Count); // Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once. CustomXmlPartCollection customXmlParts = doc.CustomXmlParts.Clone(); customXmlParts.Clear(); // Create a structured document tag that will display our part's contents and insert it into the document body. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block); tag.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", string.Empty); doc.FirstSection.Body.AppendChild(tag); doc.Save(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx"); //ExEnd Assert.IsTrue(DocumentHelper.CompareDocs(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx", GoldsDir + "StructuredDocumentTag.CustomXml Gold.docx")); doc = new Document(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx"); xmlPart = doc.CustomXmlParts[0]; Assert.True(Guid.TryParse(xmlPart.Id, out Guid temp)); Assert.AreEqual("<root><text>Hello world!</text></root>", Encoding.UTF8.GetString(xmlPart.Data)); Assert.AreEqual("http://www.w3.org/2001/XMLSchema", xmlPart.Schemas[0]); tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true); Assert.AreEqual("Hello world!", tag.GetText().Trim()); Assert.AreEqual("/root[1]/text[1]", tag.XmlMapping.XPath); Assert.AreEqual(string.Empty, tag.XmlMapping.PrefixMappings); }