Skip to content
ComPDF

Style Management

Creating and Using Styles

java
// Create a style
CStyle headingStyle = document.createStyle();

// Set style properties (supports method chaining)
headingStyle
    .setFontSize(18)
    .setFontWeight(CFontWeight.BOLD);

headingStyle.setMarginBottom(10)
    .setTextAlignment(CTextAlignment.CENTER);

// Apply the style to an element
CText heading = div.createTextElement("Heading text");
heading.addStyle(headingStyle);

// An element can also have multiple styles
CStyle colorStyle = document.createStyle();
colorStyle.setFontColor(CColor.RED);
heading.addStyle(colorStyle);