A chunk com.itextpdf.text.Chunk
is the smallest significant part that can be added to a document. A chunk is a string with Font
information.
A chunk doesn’t add line breaks, a paragraph spacing or any other spacing. The layout parameters of a chunk should be defined in the object to which this chunk is added.
When you run this example the chunk will be written from left to right. But when it reaches the right edge of the document it will start writing another chunk from the left overwriting the previous chunk.
package org.kodejava.itextpdf;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ChunkDemo {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("ChunkDemo.pdf"));
document.open();
// Add some chunks into the document object.
document.add(new Chunk("The quick brown fox "));
document.add(new Chunk("jumps over the lazy dog."));
document.add(new Chunk("The quick brown fox "));
document.add(new Chunk("jumps over the lazy dog."));
document.add(new Chunk("The quick brown fox ",
FontFactory.getFont(FontFactory.HELVETICA, 20,
Font.ITALIC, new BaseColor(0, 128, 0))));
document.add(new Chunk("jumps over the lazy dog."));
document.close();
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
document.close();
}
}
}
Maven Dependencies
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
Latest posts by Wayan (see all)
- How do I secure servlets with declarative security in web.xml - April 24, 2025
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025