Skip to content
ComPDF

Text and Paragraphs

Creating Text

java
// Create text inside the div
CText text = div.createTextElement("This is a piece of text");

// Set font properties
text.setFontFamily("Arial");
text.setFontSize(14);
text.setFontColor(new CColor(0, 0, 0, 255));
text.setFontWeight(CFontWeight.BOLD);
text.setFontStyle(CFontStyle.ITALIC);

// Set text decoration
text.setUnderline(CColor.BLACK);

// Set character and word spacing
text.setCharacterSpacing(0.5f);
text.setWordSpacing(2.0f);

Creating Paragraphs

java
CDiv div = document.createPage(800, 600).createDivElement();
// Create an empty paragraph
CParagraph paragraph0 = div.createParagraphElement();

// Or create a paragraph with initial text
CParagraph paragraph = div.createParagraphElement("Paragraph text content");

// Add text runs to the paragraph
CText text1 = paragraph.createTextElement("First part of the text");
CText text2 = paragraph.createTextElement("Second part of the text");
text2.setFontWeight(CFontWeight.BOLD);

// Set line spacing
paragraph.setFixedLeading(20f); // Fixed line spacing
// Or
paragraph.setMultipliedLeading(1.5f); // Relative line spacing (1.5x)

// Set first-line indent
paragraph.setFirstLineIndent(32f);

// Set text alignment
paragraph.setTextAlignment(CTextAlignment.JUSTIFIED);

Text Alignment Options

java
element.setTextAlignment(CTextAlignment.Left);    // Align left
element.setTextAlignment(CTextAlignment.Center);  // Center align
element.setTextAlignment(CTextAlignment.Right);   // Align right
element.setTextAlignment(CTextAlignment.Justify); // Justify