Chunk example in PDF using java iText library

In this tutorial you will learn to use the iText Chunk element in PDF document. The Chunk is the smallest significant part of text that can be added to a document.

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

In iText library there is various elements that can be added in the Document like Chunk, Paragraph etc, in all these the Chunk in the smallest element.

Most elements can be divided in one or more Chunks. A Chunk is a string with a certain Font.

In the below example you will see the how you can use the Chunk element while generating PDF using iText.

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
public class PDFChunkExample {
    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_Chunk_Example.pdf");
        // Get a PdfWriter instance to write in PDF document.
        PdfWriter.getInstance(document, out);
        // Open the PDF document;
        // Add the Chunk elements in document one by one.
        document.add(new Chunk("This is string using chunk 1. "));
        document.add(new Chunk("This is string using chunk 2. "));
        document.add(new Chunk("This is string using chunk 3. "));
        // Close the PDf document after use.

The generated PDF document of above example will looks like below screen.