com.aspose.words

Class BuildingBlockType

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

Utility class containing constants. Specifies a building block type. The type might affect the visibility and behavior of the building block in Microsoft Word.

Corresponds to the ST_DocPartType type in OOXML.

Example:

Shows how to add a custom building block to a document.
public void buildingBlockFields() throws Exception {
    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.setGlossaryDocument(glossaryDoc);

    // Create a building block and name it
    BuildingBlock block = new BuildingBlock(glossaryDoc);
    block.setName("Custom Block");

    // Put in in the document's glossary document
    glossaryDoc.appendChild(block);
    Assert.assertEquals(glossaryDoc.getCount(), 1);

    // All GUIDs are this value by default
    Assert.assertEquals(block.getGuid().toString(), "00000000-0000-0000-0000-000000000000");

    // In Microsoft Word, we can use these attributes to find blocks in Insert > Quick Parts > Building Blocks Organizer
    Assert.assertEquals(block.getCategory(), "(Empty Category)");
    Assert.assertEquals(block.getType(), BuildingBlockType.NONE);
    Assert.assertEquals(block.getGallery(), BuildingBlockGallery.ALL);
    Assert.assertEquals(block.getBehavior(), BuildingBlockBehavior.CONTENT);

    // 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.QUICK_PARTS,
            "My custom building blocks", "Custom Block");

    // Our block contains one section which now contains our text
    Assert.assertEquals(customBlock.getFirstSection().getBody().getFirstParagraph().getText(),
            "Text inside " + customBlock.getName() + '\f');
    Assert.assertEquals(customBlock.getLastSection(), customBlock.getFirstSection());

    Assert.assertNotEquals(customBlock.getGuid().toString(), "00000000-0000-0000-0000-000000000000");
    Assert.assertEquals(customBlock.getCategory(), "My custom building blocks");
    Assert.assertEquals(customBlock.getType(), BuildingBlockType.NONE);
    Assert.assertEquals(customBlock.getGallery(), BuildingBlockGallery.QUICK_PARTS);
    Assert.assertEquals(customBlock.getBehavior(), BuildingBlockBehavior.PARAGRAPH);

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

    // Or we can find it in Microsoft Word's Building Blocks Organizer and place it manually
    doc.save(getArtifactsDir() + "BuildingBlocks.BuildingBlock.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 static class BuildingBlockVisitor extends DocumentVisitor {
    public BuildingBlockVisitor(final GlossaryDocument ownerGlossaryDoc) {
        mBuilder = new StringBuilder();
        mGlossaryDoc = ownerGlossaryDoc;
    }

    public int visitBuildingBlockStart(final BuildingBlock block) {
        // Change values by default of created BuildingBlock
        block.setBehavior(BuildingBlockBehavior.PARAGRAPH);
        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.");
        block.setGallery(BuildingBlockGallery.QUICK_PARTS);

        block.setGuid(UUID.randomUUID());

        // 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.getName());
        block.getFirstSection().getBody().getFirstParagraph().appendChild(run);

        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:
BuildingBlock.Type

Field Summary
static final intNONE = 0
No type information is specified for the building block.
static final intAUTOMATICALLY_REPLACE_NAME_WITH_CONTENT = 1
Allows the building block to be automatically inserted into the document whenever its name is entered into an application.
static final intSTRUCTURED_DOCUMENT_TAG_PLACEHOLDER_TEXT = 2
The building block is a structured document tag placeholder text.
static final intFORM_FIELD_HELP_TEXT = 3
The building block is a form field help text.
static final intNORMAL = 4
The building block is a normal (i.e. regular) glossary document entry.
static final intAUTO_CORRECT = 5
The building block is associated with the spelling and grammar tools.
static final intAUTO_TEXT = 6
The building block is an AutoText entry.
static final intALL = 7
The building block is associated with all types.
static final intDEFAULT = 0
Save as NONE.
 

    • Field Detail

      • NONE = 0

        public static final int NONE
        No type information is specified for the building block.
      • AUTOMATICALLY_REPLACE_NAME_WITH_CONTENT = 1

        public static final int AUTOMATICALLY_REPLACE_NAME_WITH_CONTENT
        Allows the building block to be automatically inserted into the document whenever its name is entered into an application.
      • STRUCTURED_DOCUMENT_TAG_PLACEHOLDER_TEXT = 2

        public static final int STRUCTURED_DOCUMENT_TAG_PLACEHOLDER_TEXT
        The building block is a structured document tag placeholder text.
      • FORM_FIELD_HELP_TEXT = 3

        public static final int FORM_FIELD_HELP_TEXT
        The building block is a form field help text.
      • NORMAL = 4

        public static final int NORMAL
        The building block is a normal (i.e. regular) glossary document entry.
      • AUTO_CORRECT = 5

        public static final int AUTO_CORRECT
        The building block is associated with the spelling and grammar tools.
      • AUTO_TEXT = 6

        public static final int AUTO_TEXT
        The building block is an AutoText entry.
      • ALL = 7

        public static final int ALL
        The building block is associated with all types.
      • DEFAULT = 0

        public static final int DEFAULT
        Save as NONE.