DocumentGlossaryDocument Property

Gets or sets the glossary document within this document or template. A glossary document is a storage for AutoText, AutoCorrect and Building Block entries defined in a document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.8.0


public GlossaryDocument GlossaryDocument { get; set; }

Property Value

Type: GlossaryDocument


This property returns null if the document does not have a glossary document.

You can add a glossary document to a document by creating a GlossaryDocument object and assigning to this property.


Shows how to add a custom building block to a document.
public void BuildingBlockFields()
    Document doc = new Document();

    // BuildingBlocks are stored 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
    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);

    // 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);

    // 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(ArtifactsDir + "BuildingBlocks.BuildingBlockFields.dotx");

/// <summary>
/// Simple implementation of adding text to a building block and preparing it for usage in the document. Implemented as a Visitor.
/// </summary>
public class BuildingBlockVisitor : DocumentVisitor
    public BuildingBlockVisitor(GlossaryDocument ownerGlossaryDoc)
        mBuilder = new StringBuilder();
        mGlossaryDoc = ownerGlossaryDoc;

    public override VisitorAction VisitBuildingBlockStart(BuildingBlock block)
        // Change values by default of created BuildingBlock
        block.Behavior = BuildingBlockBehavior.Paragraph;
        block.Category = "My custom building blocks";
        block.Description =
            "Using this block in the Quick Parts section of word will place its contents at the cursor.";
        block.Gallery = BuildingBlockGallery.QuickParts;

        block.Guid = Guid.NewGuid();

        // Add content for the BuildingBlock to have an effect when used in the document
        Section section = new Section(mGlossaryDoc);

        Body body = new Body(mGlossaryDoc);

        Paragraph paragraph = new Paragraph(mGlossaryDoc);

        // Add text that will be visible in the document
        Run run = new Run(mGlossaryDoc, "Text inside " + block.Name);

        return VisitorAction.Continue;

    public override VisitorAction VisitBuildingBlockEnd(BuildingBlock block)
        mBuilder.Append("Visited " + block.Name + "\r\n");
        return VisitorAction.Continue;

    private readonly StringBuilder mBuilder;
    private readonly GlossaryDocument mGlossaryDoc;

See Also