[Test] //ExSkip public void GlossaryDocument() { Document doc = new Document(); GlossaryDocument glossaryDoc = new GlossaryDocument(); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 1" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 2" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 3" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 4" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 5" }); Assert.AreEqual(5, glossaryDoc.BuildingBlocks.Count); doc.GlossaryDocument = glossaryDoc; // There is a different ways how to get created building blocks Assert.AreEqual("Block 1", glossaryDoc.FirstBuildingBlock.Name); Assert.AreEqual("Block 2", glossaryDoc.BuildingBlocks[1].Name); Assert.AreEqual("Block 3", glossaryDoc.BuildingBlocks.ToArray()[2].Name); Assert.AreEqual("Block 5", glossaryDoc.LastBuildingBlock.Name); // Get a block by gallery, category and name BuildingBlock block4 = glossaryDoc.GetBuildingBlock(BuildingBlockGallery.All, "(Empty Category)", "Block 4"); // All GUIDs are the same by default Assert.AreEqual("00000000-0000-0000-0000-000000000000", block4.Guid.ToString()); // To be able to uniquely identify blocks by GUID, each GUID must be unique // We will do that using a custom visitor GlossaryDocVisitor visitor = new GlossaryDocVisitor(); glossaryDoc.Accept(visitor); Assert.AreEqual(5, visitor.GetDictionary().Count); Console.WriteLine(visitor.GetText()); // We can find our new blocks in Microsoft Word via Insert > Quick Parts > Building Blocks Organizer... doc.Save(MyDir + @"\Artifacts\BuildingBlocks.GlossaryDocument.dotx"); }
[Test] //ExSkip public void GlossaryDocument() { Document doc = new Document(); GlossaryDocument glossaryDoc = new GlossaryDocument(); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 1" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 2" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 3" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 4" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 5" }); Assert.AreEqual(5, glossaryDoc.BuildingBlocks.Count); doc.GlossaryDocument = glossaryDoc; // There are various ways of accessing building blocks. // 1 - Get the first/last building blocks in the collection: Assert.AreEqual("Block 1", glossaryDoc.FirstBuildingBlock.Name); Assert.AreEqual("Block 5", glossaryDoc.LastBuildingBlock.Name); // 2 - Get a building block by index: Assert.AreEqual("Block 2", glossaryDoc.BuildingBlocks[1].Name); Assert.AreEqual("Block 3", glossaryDoc.BuildingBlocks.ToArray()[2].Name); // 3 - Get the first building block that matches a gallery, name and category: Assert.AreEqual("Block 4", glossaryDoc.GetBuildingBlock(BuildingBlockGallery.All, "(Empty Category)", "Block 4").Name); // We will do that using a custom visitor, // which will give every BuildingBlock in the GlossaryDocument a unique GUID GlossaryDocVisitor visitor = new GlossaryDocVisitor(); glossaryDoc.Accept(visitor); Assert.AreEqual(5, visitor.GetDictionary().Count); //ExSkip Console.WriteLine(visitor.GetText()); // When we open this document using Microsoft Word, // we can find the building blocks via Insert -> Quick Parts -> Building Blocks Organizer. doc.Save(ArtifactsDir + "BuildingBlocks.GlossaryDocument.dotx"); }
[Test] //ExSkip public void GlossaryDocument() { Document doc = new Document(); GlossaryDocument glossaryDoc = new GlossaryDocument(); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 1" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 2" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 3" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 4" }); glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 5" }); Assert.AreEqual(5, glossaryDoc.BuildingBlocks.Count); doc.GlossaryDocument = glossaryDoc; // There is a different ways how to get created building blocks Assert.AreEqual("Block 1", glossaryDoc.FirstBuildingBlock.Name); Assert.AreEqual("Block 2", glossaryDoc.BuildingBlocks[1].Name); Assert.AreEqual("Block 3", glossaryDoc.BuildingBlocks.ToArray()[2].Name); Assert.AreEqual("Block 4", glossaryDoc.GetBuildingBlock(BuildingBlockGallery.All, "(Empty Category)", "Block 4").Name); Assert.AreEqual("Block 5", glossaryDoc.LastBuildingBlock.Name); // We will do that using a custom visitor, which also will give every BuildingBlock in the GlossaryDocument a unique GUID GlossaryDocVisitor visitor = new GlossaryDocVisitor(); glossaryDoc.Accept(visitor); Assert.AreEqual(5, visitor.GetDictionary().Count); //ExSkip Console.WriteLine(visitor.GetText()); // We can find our new blocks in Microsoft Word via Insert > Quick Parts > Building Blocks Organizer... doc.Save(ArtifactsDir + "BuildingBlocks.GlossaryDocument.dotx"); }
public void PlaceholderBuildingBlock(bool isShowingPlaceholderText) { //ExStart //ExFor:StructuredDocumentTag.IsShowingPlaceholderText //ExFor:StructuredDocumentTag.Placeholder //ExFor:StructuredDocumentTag.PlaceholderName //ExSummary:Shows how to use a building block's contents as a custom placeholder text for a structured document tag. Document doc = new Document(); // Insert a plain text structured document tag of the "PlainText" type, which will function as a text box. // The contents that it will display by default are a "Click here to enter text." prompt. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline); // We can get the tag to display the contents of a building block instead of the default text. // First, add a building block with contents to the glossary document. GlossaryDocument glossaryDoc = doc.GlossaryDocument; BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc); substituteBlock.Name = "Custom Placeholder"; substituteBlock.AppendChild(new Section(glossaryDoc)); substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc)); substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text."); glossaryDoc.AppendChild(substituteBlock); // Then, use the structured document tag's "PlaceholderName" property to reference that building block by name. tag.PlaceholderName = "Custom Placeholder"; // If "PlaceholderName" refers to an existing block in the parent document's glossary document, // we will be able to verify the building block via the "Placeholder" property. Assert.AreEqual(substituteBlock, tag.Placeholder); // Set the "IsShowingPlaceholderText" property to "true" to treat the // structured document tag's current contents as placeholder text. // This means that clicking on the text box in Microsoft Word will immediately highlight all the tag's contents. // Set the "IsShowingPlaceholderText" property to "false" to get the // structured document tag to treat its contents as text that a user has already entered. // Clicking on this text in Microsoft Word will place the blinking cursor at the clicked location. tag.IsShowingPlaceholderText = isShowingPlaceholderText; DocumentBuilder builder = new DocumentBuilder(doc); builder.InsertNode(tag); doc.Save(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx"); //ExEnd doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx"); tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true); substituteBlock = (BuildingBlock)doc.GlossaryDocument.GetChild(NodeType.BuildingBlock, 0, true); Assert.AreEqual("Custom Placeholder", substituteBlock.Name); Assert.AreEqual(isShowingPlaceholderText, tag.IsShowingPlaceholderText); Assert.AreEqual(substituteBlock, tag.Placeholder); Assert.AreEqual(substituteBlock.Name, tag.PlaceholderName); }
public void PlaceholderBuildingBlock() { //ExStart //ExFor:StructuredDocumentTag.IsShowingPlaceholderText //ExFor:StructuredDocumentTag.Placeholder //ExFor:StructuredDocumentTag.PlaceholderName //ExSummary:Shows how to use the contents of a BuildingBlock as a custom placeholder text for a StructuredDocumentTag. Document doc = new Document(); // Insert a plain text StructuredDocumentTag of the PlainText type, which will function like a text box // It contains a default "Click here to enter text." prompt, which we can click and replace with our own text StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline); // We can substitute that default placeholder with a custom phrase, which will be drawn from a BuildingBlock // First, we will need to create the BuildingBlock, give it content and add it to the GlossaryDocument GlossaryDocument glossaryDoc = doc.GlossaryDocument; BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc); substituteBlock.Name = "Custom Placeholder"; substituteBlock.AppendChild(new Section(glossaryDoc)); substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc)); substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text."); glossaryDoc.AppendChild(substituteBlock); // The substitute BuildingBlock we made can be referenced by name tag.PlaceholderName = "Custom Placeholder"; // If PlaceholderName refers to an existing block in the parent document's GlossaryDocument, // the BuildingBlock will be automatically found and assigned to the Placeholder attribute Assert.AreEqual(substituteBlock, tag.Placeholder); // Setting this to true will register the text inside the StructuredDocumentTag as placeholder text // This means that, in Microsoft Word, all the text contents of the StructuredDocumentTag will be highlighted with one click, // so we can immediately replace the entire substitute text by typing // If this is false, the text will behave like an ordinary Paragraph and a cursor will be placed with nothing highlighted tag.IsShowingPlaceholderText = true; // Insert the StructuredDocumentTag into the document using a DocumentBuilder and save the document to a file DocumentBuilder builder = new DocumentBuilder(doc); builder.InsertNode(tag); doc.Save(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx"); //ExEnd doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx"); tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true); substituteBlock = (BuildingBlock)doc.GlossaryDocument.GetChild(NodeType.BuildingBlock, 0, true); Assert.AreEqual("Custom Placeholder", substituteBlock.Name); Assert.True(tag.IsShowingPlaceholderText); Assert.AreEqual(substituteBlock, tag.Placeholder); Assert.AreEqual(substituteBlock.Name, tag.PlaceholderName); }
[Test] //ExSkip public void BuildingBlockFields() { Document doc = new Document(); // BuildingBlocks live inside the glossary document // If you're making a document from scratch, the glossary document must also be manually created GlossaryDocument glossaryDoc = new GlossaryDocument(); doc.GlossaryDocument = glossaryDoc; // Create a building block and name it BuildingBlock block = new BuildingBlock(glossaryDoc); block.Name = "Custom Block"; // Put in in the document's glossary document glossaryDoc.AppendChild(block); Assert.AreEqual(1, glossaryDoc.Count); // All GUIDs are this value by default Assert.AreEqual("00000000-0000-0000-0000-000000000000", block.Guid.ToString()); // In Microsoft Word, we can use these attributes to find blocks in Insert > Quick Parts > Building Blocks Organizer Assert.AreEqual("(Empty Category)", block.Category); Assert.AreEqual(BuildingBlockType.None, block.Type); Assert.AreEqual(BuildingBlockGallery.All, block.Gallery); Assert.AreEqual(BuildingBlockBehavior.Content, block.Behavior); // If we want to use our building block as an AutoText quick part, we need to give it some text and change some properties // All the necessary preparation will be done in a custom document visitor that we will accept BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc); block.Accept(visitor); // We can find the block we made in the glossary document like this BuildingBlock customBlock = glossaryDoc.GetBuildingBlock(BuildingBlockGallery.QuickParts, "My custom building blocks", "Custom Block"); // Our block contains one section which now contains our text Assert.AreEqual("Text inside " + customBlock.Name + '\f', customBlock.FirstSection.Body.FirstParagraph.GetText()); Assert.AreEqual(customBlock.FirstSection, customBlock.LastSection); Assert.AreNotEqual("00000000-0000-0000-0000-000000000000", customBlock.Guid.ToString()); Assert.AreEqual("My custom building blocks", customBlock.Category); Assert.AreEqual(BuildingBlockType.None, customBlock.Type); Assert.AreEqual(BuildingBlockGallery.QuickParts, customBlock.Gallery); Assert.AreEqual(BuildingBlockBehavior.Paragraph, customBlock.Behavior); // Then we can insert it into the document as a new section doc.AppendChild(doc.ImportNode(customBlock.FirstSection, true)); // Or we can find it in Microsoft Word's Building Blocks Organizer and place it manually doc.Save(MyDir + @"\Artifacts\BuildingBlocks.BuildingBlock.dotx"); }
[Test] //ExSkip public void CreateAndInsert() { // A document's glossary document stores building blocks. Document doc = new Document(); GlossaryDocument glossaryDoc = new GlossaryDocument(); doc.GlossaryDocument = glossaryDoc; // Create a building block, name it, and then add it to the glossary document. BuildingBlock block = new BuildingBlock(glossaryDoc) { Name = "Custom Block" }; glossaryDoc.AppendChild(block); // All new building block GUIDs have the same zero value by default, and we can give them a new unique value. Assert.AreEqual("00000000-0000-0000-0000-000000000000", block.Guid.ToString()); block.Guid = Guid.NewGuid(); // The following attributes categorize building blocks // in the menu found via Insert -> Quick Parts -> Building Blocks Organizer in Microsoft Word. Assert.AreEqual("(Empty Category)", block.Category); Assert.AreEqual(BuildingBlockType.None, block.Type); Assert.AreEqual(BuildingBlockGallery.All, block.Gallery); Assert.AreEqual(BuildingBlockBehavior.Content, block.Behavior); // Before we can add this building block to our document, we will need to give it some contents. // We will do that and set a category, gallery, and behavior with a document visitor. BuildingBlockVisitor visitor = new BuildingBlockVisitor(glossaryDoc); block.Accept(visitor); // We can access the block that we just made from the glossary document. BuildingBlock customBlock = glossaryDoc.GetBuildingBlock(BuildingBlockGallery.QuickParts, "My custom building blocks", "Custom Block"); // The block itself is a section that contains the text. Assert.AreEqual($"Text inside {customBlock.Name}\f", customBlock.FirstSection.Body.FirstParagraph.GetText()); Assert.AreEqual(customBlock.FirstSection, customBlock.LastSection); Assert.DoesNotThrow(() => Guid.Parse(customBlock.Guid.ToString())); //ExSkip Assert.AreEqual("My custom building blocks", customBlock.Category); //ExSkip Assert.AreEqual(BuildingBlockType.None, customBlock.Type); //ExSkip Assert.AreEqual(BuildingBlockGallery.QuickParts, customBlock.Gallery); //ExSkip Assert.AreEqual(BuildingBlockBehavior.Paragraph, customBlock.Behavior); //ExSkip // Now, we can insert it into the document as a new section. doc.AppendChild(doc.ImportNode(customBlock.FirstSection, true)); // We can also find it in Microsoft Word's Building Blocks Organizer and place it manually. doc.Save(ArtifactsDir + "BuildingBlocks.CreateAndInsert.dotx"); }
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 }