How do I define a font for text object in iText?

We can set font style for text object such as Chunk, Phrase, Paragraph, etc. using the com.itextpdf.text.Font class. We can define the font face, size, style and its color using this class.

package org.kodejava.itextpdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;


public class FontDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("FontDemo.pdf"));

            // Creates some fonts
            Font largeBold = new Font(Font.FontFamily.COURIER, 32,
            Font smallItalic = new Font(Font.FontFamily.HELVETICA, 10,
            Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
                    Font.ITALIC | Font.UNDERLINE, BaseColor.RED);

            // Creates chunk, phrase and paragraph with font
            // information.
            Chunk chunk = new Chunk("Hello World", largeBold);
            Phrase phrase =
                    new Phrase("The quick brown fox ", smallItalic);
            Paragraph paragraph =
                    new Paragraph("jumps over the lazy dog", redFont);

        } catch (DocumentException | FileNotFoundException e) {
        } finally {

Maven Dependencies


Maven Central

How do I set the font and color of JTextArea?

To set the font and color of JTextArea we can use the setFont() and setForeground() methods of the JTextArea. To create a font we must define the font name, the font style and its size. For the colors we can uses the constant color values defined by the Color class.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class TextAreaFontDemo extends JPanel {
    public TextAreaFontDemo() {

    private static void showFrame() {
        JPanel panel = new TextAreaFontDemo();

        JFrame frame = new JFrame("JTextArea Demo");

    public static void main(String[] args) {

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(500, 200));

        JTextArea textArea = new JTextArea(5, 40);
        textArea.setText("The quick brown fox jumps over the lazy dog.");

        // Sets JTextArea font and color.
        Font font = new Font("Segoe Script", Font.BOLD, 20);
        JScrollPane scrollPane = new JScrollPane(textArea);

        this.add(scrollPane, BorderLayout.CENTER);

Here is the program screenshot of the code snippet above:

JTextArea Font and Color Demo

How do I get the available font family names?

package org.kodejava.awt;

import java.awt.GraphicsEnvironment;

public class FontFamilyNameList {
    public static void main(String[] args) {
        // Get all available font family names from GraphicsEnvironment
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] familyNames = ge.getAvailableFontFamilyNames();

        // Iterates familyNames array to display the available font's family names
        for (String familyName : familyNames) {
            System.out.println("Family name: " + familyName);

Some font family names are shown below:

Family name: Agency FB
Family name: Algerian
Family name: Arial
Family name: Arial Black
Family name: Arial Narrow
Family name: Arial Rounded MT Bold
Family name: Bahnschrift
Family name: Baskerville Old Face
Family name: Bauhaus 93
Family name: Bell MT

How do I get the available font names?

package org.kodejava.awt;

import java.awt.Font;
import java.awt.GraphicsEnvironment;

public class FontNameList {
    public static void main(String[] args) {
        // Get all available fonts from GraphicsEnvironment
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = ge.getAllFonts();

        // Iterates all available fonts and get their name and family name
        for (Font font : fonts) {
            String fontName = font.getName();
            String familyName = font.getFamily();

            System.out.println("Font: " + fontName + "; family: " + familyName);

Here are some fonts name printed from the code snippet above:

Font: Agency FB; family: Agency FB
Font: Agency FB Bold; family: Agency FB
Font: Algerian; family: Algerian
Font: Arial; family: Arial
Font: Arial Black; family: Arial Black
Font: Arial Bold; family: Arial
Font: Arial Bold Italic; family: Arial
Font: Arial Italic; family: Arial
Font: Arial Narrow; family: Arial Narrow
Font: Arial Narrow Bold; family: Arial Narrow