How to set font of text in PDF using java iText library

In this tutorial you will learn how to use Fonts while generating text in PDF using iText java library. You can specify fonts for most text objects like Chunk, Phrase or Paragraph in IText.

Please note we are using the iText version 5 for this example.

But you can much more with fonts in iText what has been shown in this example.

There is a class com.lowagie.text.Font. To use a font you should create the font first, and you can pass the created font it to text elements like Chunk, Phrase or Paragraph.

Below is the example of code which shows you to use of Fonts in iText.

package com.java.connect.itext;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
 
public class PDFFontExample {
    public static void main(String[] args) throws FileNotFoundException, DocumentException {
 
        // Create new PDF document
        Document document = new Document();
        // Create an output stream of PDF file.
        OutputStream out = new FileOutputStream("PDF_Font_Example.pdf");
        // Get a PdfWriter instance to write in PDF document.
        PdfWriter.getInstance(document, out);
        // Open the PDF document
        document.open();
        // Create the Fonts
        Font normalFont = new Font(Font.NORMAL, 12);
        Font normalBoldFont = new Font(Font.NORMAL, 12, Font.BOLD);
        Font courierFontItalic = new Font(Font.COURIER, 10, Font.ITALIC);
        Font courierFontUnderline = new Font(Font.COURIER, 10, Font.UNDERLINE);
        Font timesRomanItalic = new Font(Font.TIMES_ROMAN, 20, Font.ITALIC);
        Font timesRomanUnderline = new Font(Font.TIMES_ROMAN, 20, Font.UNDERLINE);
        // Apply the created fonts to Chunks, Phrases and Paragraphs.
        document.add(new Chunk("This is Normal text. ", normalFont));
        document.add(new Chunk("This is Normal Bold text.  ", normalBoldFont));
        document.add(new Phrase("This is Courier Italic text.  ", courierFontItalic));
        document.add(new Phrase("This is Courier Underline text.  ", courierFontUnderline));
        document.add(new Paragraph("This is Times Roman Italic text.  ", timesRomanItalic));
        document.add(new Paragraph("This is Times Roman Underline text.  ", timesRomanUnderline));
        // Close the document after use.
        document.close();
 
    }
}

The generated PDF document looks like below screen.

iText-font-in-text-example-in-PDF-using-java-iText-library_files