Пример #1
0
        [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");
        }
Пример #2
0
        [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");
        }