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.
Example:
Shows how to add a custom building block to a document.
public void buildingBlockFields() throws Exception {
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.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(MessageFormat.format("Text inside {0}\f", customBlock.getName()), customBlock.getFirstSection().getBody().getFirstParagraph().getText());
Assert.assertEquals(customBlock.getFirstSection(), customBlock.getLastSection());
// 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.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 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.Behavior
Field Summary |
static final int | CONTENT = 0 | |
Specifies that the building block shall be inserted as inline content.
|
static final int | PARAGRAPH = 1 | |
Specifies that the building block shall be inserted into its own paragraph.
|
static final int | PAGE = 2 | |
Specifies that the building block shall be added into its own page.
|
static final int | DEFAULT = 0 | |
Same as CONTENT.
|
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.
See Also:
Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
Aspose.Words Support Forum - our preferred method of support.