java iText chunk

The Chunk represents the smallest unit of text as a string with pre-defined font. It is represented by com.itextpdf.text.Chunk class.
Note: The Chunk objects not add any space or line break.


1. Create Document instance. It represents the current document to which we are adding content.
2. Create OutputStream instance. It represents the generated pdf.
3. Create PDFWriter instance and pass Document and OutputStream instance to its constructor.
4. Open the Document by calling
5. Add the content to the document by calling document.add() method by using Chunk objects.
6. Close the document by calling document.close() method.

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
* This class is used to create a pdf file using iText jar.
* @author javawithease

public class PDFChunkExample {
public static void main(String args[]){
try {
//Create Document instance.
Document document = new Document();
//Create OutputStream instance.
OutputStream outputStream =
new FileOutputStream(new File("D:\\TestChunkFile.pdf"));
//Create PDFWriter instance.
PdfWriter.getInstance(document, outputStream);
//Open the document.;
//Add content to the document using chunk objects.
document.add(new Chunk("Test chunk1."));
document.add(new Chunk("Test chunk2."));
//Close document and outputStream.
System.out.println("Pdf created successfully.");
} catch (Exception e) {


Pdf created successfully.

No comments: