[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 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"); }
[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"); }