How do I style some texts in the Paragraph object in iText 8?

In iText 8, if you want to style only part of the text inside a Paragraph object, you can use the Text object to encapsulate the text that needs separate styling.

Here’s an example:

package org.kodejava.itext;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.io.font.constants.StandardFonts;

public class ParagraphTextExample {
    public static void main(String[] args) throws Exception {
        PdfWriter writer = new PdfWriter("styled-text.pdf");
        PdfDocument pdf = new PdfDocument(writer);

        try (Document document = new Document(pdf)) {

            PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);

            // Create a Text object and style it
            Text text1 = new Text("This text is green and super bold. ")
                    .setFont(font)
                    .setFontSize(15)
                    .setFontColor(new DeviceRgb(0, 255, 0))
                    .setBold();

            // Create another Text object and style it differently
            Text text2 = new Text("This text is blue and italic. ")
                    .setFont(font)
                    .setFontSize(12)
                    .setFontColor(new DeviceRgb(0, 0, 255))
                    .setItalic();

            // Create another Text object and style it differently
            final String text = """
                    What's in a name? \
                    That which we call a rose by any other name \
                    would smell just as sweet.
                    """;
            Text text3 = new Text(text)
                    .setFont(font)
                    .setFontSize(20)
                    .setFontColor(new DeviceRgb(243, 58, 106))
                    .setBold()
                    .setItalic();

            // Add Text objects to a single Paragraph
            Paragraph paragraph = new Paragraph().add(text1).add(text2).add(text3);

            document.add(paragraph);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In this example, three Text objects, text1, text2, and text3, are created with different styles. The Text objects are then added to a Paragraph object. As a result, the paragraph will have mixed styles, depending on the individual text elements.

Notably, you can apply a rich variety of styling on the Text objects, including font, size, color, background color, underlines, strike-through, superscripts, subscripts, and nearly any other style applicable to text.

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-core</artifactId>
    <version>8.0.4</version>
    <type>pom</type>
</dependency>

Maven Central

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.