public class ConditionalStyleType
Example:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a table
Table table = builder.startTable();
builder.insertCell();
builder.write("Cell 1");
builder.insertCell();
builder.write("Cell 2");
builder.endRow();
builder.insertCell();
builder.write("Cell 3");
builder.insertCell();
builder.write("Cell 4");
builder.endTable();
// Create a custom table style
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1");
// Conditional styles are formatting changes that affect only some of the cells of the table based on a predicate,
// such as the cells being in the last row
// We can access these conditional styles by style type like this
tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.FIRST_ROW).getShading().setBackgroundPatternColor(Color.BLUE);
// The same conditional style can be accessed by index
tableStyle.getConditionalStyles().get(0).getBorders().setColor(Color.BLACK);
tableStyle.getConditionalStyles().get(0).getBorders().setLineStyle(LineStyle.DOT_DASH);
Assert.assertEquals(ConditionalStyleType.FIRST_ROW, tableStyle.getConditionalStyles().get(0).getType());
// It can also be found in the ConditionalStyles collection as an attribute
tableStyle.getConditionalStyles().getFirstRow().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// Apply padding and text formatting to conditional styles
tableStyle.getConditionalStyles().getLastRow().setBottomPadding(10.0);
tableStyle.getConditionalStyles().getLastRow().setLeftPadding(10.0);
tableStyle.getConditionalStyles().getLastRow().setRightPadding(10.0);
tableStyle.getConditionalStyles().getLastRow().setTopPadding(10.0);
tableStyle.getConditionalStyles().getLastColumn().getFont().setBold(true);
// List all possible style conditions
Iterator<ConditionalStyle> enumerator = tableStyle.getConditionalStyles().iterator();
while (enumerator.hasNext()) {
ConditionalStyle currentStyle = enumerator.next();
if (currentStyle != null) System.out.println(currentStyle.getType());
}
// Apply conditional style to the table
table.setStyle(tableStyle);
// Changes to the first row are enabled by the table's style options be default,
// but need to be manually enabled for some other parts, such as the last column/row
table.setStyleOptions(table.getStyleOptions() | TableStyleOptions.LAST_ROW | TableStyleOptions.LAST_COLUMN);
doc.save(getArtifactsDir() + "Table.ConditionalStyles.docx");
Field Summary | ||
---|---|---|
static final int | FIRST_ROW | |
Specifies formatting of the first row of a table.
|
||
static final int | FIRST_COLUMN | |
Specifies formatting of the first column of a table.
|
||
static final int | LAST_ROW | |
Specifies formatting of the last row of a table.
|
||
static final int | LAST_COLUMN | |
Specifies formatting of the last column of a table.
|
||
static final int | ODD_ROW_BANDING | |
Specifies formatting of odd-numbered row stripe.
|
||
static final int | ODD_COLUMN_BANDING | |
Specifies formatting of odd-numbered column stripe.
|
||
static final int | EVEN_ROW_BANDING | |
Specifies formatting of even-numbered row stripe.
|
||
static final int | EVEN_COLUMN_BANDING | |
Specifies formatting of even-numbered column stripe.
|
||
static final int | TOP_LEFT_CELL | |
Specifies formatting of the top left cell of a table.
|
||
static final int | TOP_RIGHT_CELL | |
Specifies formatting of the top right cell of a table.
|
||
static final int | BOTTOM_LEFT_CELL | |
Specifies formatting of the bottom left cell of a table.
|
||
static final int | BOTTOM_RIGHT_CELL | |
Specifies formatting of the bottom right cell of a table.
|
public static final int FIRST_ROW
public static final int FIRST_COLUMN
public static final int LAST_ROW
public static final int LAST_COLUMN
public static final int ODD_ROW_BANDING
public static final int ODD_COLUMN_BANDING
public static final int EVEN_ROW_BANDING
public static final int EVEN_COLUMN_BANDING
public static final int TOP_LEFT_CELL
public static final int TOP_RIGHT_CELL
public static final int BOTTOM_LEFT_CELL
public static final int BOTTOM_RIGHT_CELL