Home iText PDF tutorials

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.

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.

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.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
		document.open();
		// 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.
		document.close();
	}
}

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