Class BuildingBlockBehavior

  • java.lang.Object
    • com.aspose.words.BuildingBlockBehavior
public class BuildingBlockBehavior 
extends java.lang.Object

Utility class containing constants. Specifies the behavior that shall be applied to the contents of the building block when it is inserted into the main document.

Corresponds to the ST_DocPartBehavior type in OOXML.


Shows how to add a custom building block to a document.
public void createAndInsert() throws Exception
    // A document's glossary document stores building blocks.
    Document doc = new Document();
    GlossaryDocument glossaryDoc = new GlossaryDocument();

    // Create a building block, name it, and then add it to the glossary document.
    BuildingBlock block = new BuildingBlock(glossaryDoc);
    block.setName("Custom Block");


    // All new building block GUIDs have the same zero value by default, and we can give them a new unique value.
    Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");


    // The following attributes categorize building blocks
    // in the menu found via Insert -> Quick Parts -> Building Blocks Organizer in Microsoft Word.
    Assert.assertEquals(block.getCategory(), "(Empty Category)");
    Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
    Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
    Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);

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

    // We can access the block that we just made from the glossary document.
    BuildingBlock customBlock = glossaryDoc.getBuildingBlock(BuildingBlockGallery.QUICK_PARTS,
            "My custom building blocks", "Custom Block");

    // The block itself is a section that contains the text.
    Assert.assertEquals(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
    Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());

    // Now, we can insert it into the document as a new section.
    doc.appendChild(doc.importNode(customBlock.getFirstSection(), true));

    // We can also find it in Microsoft Word's Building Blocks Organizer and place it manually. + "BuildingBlocks.CreateAndInsert.dotx");

/// <summary>
/// Sets up a visited building block to be inserted into the document as a quick part and adds text to its contents.
/// </summary>
public static class BuildingBlockVisitor extends DocumentVisitor {
    public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
        mBuilder = new StringBuilder();
        mGlossaryDoc = ownerGlossaryDoc;

    public int visitBuildingBlockStart(final BuildingBlock block) {
        // Configure the building block as a quick part, and add attributes used by Building Blocks Organizer.
        block.setCategory("My custom building blocks");
        block.setDescription("Using this block in the Quick Parts section of word will place its contents at the cursor.");

        // Add a section with text.
        // Inserting the block into the document will append this section with its child nodes at the location.
        Section section = new Section(mGlossaryDoc);

        Run run = new Run(mGlossaryDoc, "Text inside " + block.getName());

        return VisitorAction.CONTINUE;

    public int visitBuildingBlockEnd(final BuildingBlock block) {
        mBuilder.append("Visited " + block.getName() + "\r\n");
        return VisitorAction.CONTINUE;

    private StringBuilder mBuilder;
    private GlossaryDocument mGlossaryDoc;
See Also:

Field Summary
static final intCONTENT = 0
Specifies that the building block shall be inserted as inline content.
static final intPARAGRAPH = 1
Specifies that the building block shall be inserted into its own paragraph.
static final intPAGE = 2
Specifies that the building block shall be added into its own page.
static final intDEFAULT = 0
Same as CONTENT.

    • Field Detail

      • CONTENT = 0

        public static final int CONTENT
        Specifies that the building block shall be inserted as inline content.
      • PARAGRAPH = 1

        public static final int PARAGRAPH
        Specifies that the building block shall be inserted into its own paragraph.
      • PAGE = 2

        public static final int PAGE
        Specifies that the building block shall be added into its own page.
      • DEFAULT = 0

        public static final int DEFAULT
        Same as CONTENT.