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 get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024