How to Phrase PDF using java iText library

In this tutorial you will learn how to use the Phrase element in PDF document. The Phrase is series of Chunks.

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

The Phrase class in IText represents a “phrase” of text. The Phrase class knows how to add spacing between lines.

Below is the code of PDF document generation which use the Phrase elements.

package com.java.connect.itext;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
 
public class PDFPhraseExample {
    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_Phrase_Example.pdf");
        // Get a PdfWriter instance to write in PDF document.
        PdfWriter.getInstance(document, out);
        // Open the PDF document
        document.open();
        // Create and add Phrase in PDF documents
        document.add(new Phrase("This is string phrase 1. "));
        document.add(new Phrase("This is string phrase 2. "));
        document.add(new Phrase("This is string phrase 3. "));
        document.add(new Phrase("This is string phrase 4. "));
        // Close the PDf document after use.
        document.close();
    }
}

Below is screen shot how the generated PDF document looks like.

iText-Phrase-example-in-PDF-using-java-iText-library_files