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
<!-- https://search.maven.org/remotecontent?filepath=com/itextpdf/itextpdf/5.5.13.3/itextpdf-5.5.13.3.jar -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023