Practical javadocx

Blocks

Introduction

Blocks let you group elements and easily replace and delete them. They can be created with MS Word or any other DOCX editor, even with the available methods of the javadocx API.

Blocks can contain texts, images, placeholders to be replaced with javadocx, sub-blocks and almost any other content.

Defining elements of the block type

To define blocks in a DOCX, create two placeholders with the same name, at the beginning and the end of the block content, with the following syntaxis:

Replace * with the block name, e.g:

When invoking block methods it is mandatory to specify the block name without neither prefix ($BLOCK by default) nor suffix. So, to delete the A1 block execute:

Replace values

Existing placeholders in a block can be replaced with javadocx methods with no need of indicating block names. E.g., if a block has the $VAR1$ placeholder, it is possible to replace it with replaceVariableByText:

Remove and clean

The deleteTemplateBlock method deletes the whole content of a block. With this functionality it is possible to define optional blocks that can be deleted if they are unnecessary:

clearBlocks removes just the blocks placeholders while keeping their content. E.g., to replace contents and then cleaning the placeholders that wrap the blocks:

Tips and Tricks

DOCXPath lets you perform more complex replacements, such as substituting placeholders by name or its position in the block that contains them.

Next - Document transformations