In this iText 7 example, we need to switch between different styles in the same document:
The best way to achieve this in iText 7, is to create a Style
object, and to apply that Style
to a Text
object:
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFont code = PdfFontFactory.createFont(FontConstants.COURIER);
Style style = new Style()
.setFont(code)
.setFontSize(12)
.setFontColor(Color.RED)
.setBackgroundColor(Color.LIGHT_GRAY);
try (Document document = new Document(pdf)) {
document.add(
new Paragraph()
.add("In this example, named ")
.add(new Text("HelloWorldStyles").addStyle(style))
.add(", we experiment with some text in ")
.add(new Text("code style").addStyle(style))
.add("."));
}
}
Source: developers.itextpdf.com and the iText 7: Building Blocks tutorial.