BuildingBlockName Property

Gets or sets the name of this building block.

Namespace:  Aspose.Words.BuildingBlocks
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public string Name { get; set; }

Property Value

Type: String
Remarks

The name may contain any string content, usually a friendly identifier. Multiple building blocks can have the same name.

Cannot be null and cannot be an empty string.

Corresponds to the docPartPr.name element in OOXML.

Examples
Shows how to add a custom building block to a document.
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(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);
        block.AppendChild(section);

        Body body = new Body(mGlossaryDoc);
        section.AppendChild(body);

        Paragraph paragraph = new Paragraph(mGlossaryDoc);
        body.AppendChild(paragraph);

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

        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