How to create Paragraph in PDF using java iText library

In this tutorial you will learn how to add Paragraph while generating PDF using iText library. The Paragraph is a series of Chunks or Phrases.

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

The Paragraph has the same qualities of a Phrase in addition of some additional layout-parameters. In a paragraph you can set the paragraph alignment, indentation and spacing before and after the paragraph.

The below code sample use the Chunks of Paragraph.

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.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
 
public class PDFParagraphExample {
    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_Paragraph_example.pdf");
        // Get a PdfWriter instance to write in PDF document.
        PdfWriter.getInstance(document, out);
        // Open the PDF document
        document.open();
        // Create the Paragraph and add Chunks to paragraph.
        Paragraph paragraph = new Paragraph();
        for (int i = 0; i < 10; i++) {
            // Add some chunks in paragraph.
            Chunk chunk = new Chunk("This is a string part of chunk " + i + ". ");
            paragraph.add(chunk);
        }
        // Add the Paragraph to document.
        document.add(paragraph);
        // Close the PDf document after use.
        document.close();
    }
}

The generated PDF document looks like below screen.

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