itextStyles: iText 5 versus iText 7


Remarks

Creating a document in which you have to switch between styles frequently tends to be tedious in iText 5. You need to create a lot of Chunk objects and you always have to make a trade-off between applying the styles directly to every new Chunk or creating a helper method that creates the Chunk for you.

What we fixed in iText 7:

It is now possible to chain methods. The setFont(), setFontSize(), addStyle(), and other methods all return the object on which they are invoked. Adding a Paragraph involving different styles can now be done in one line:

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("."));

Using the Style object, you can now also apply different properties (font, font color, background color, font size,...) in one go with the addStyle() method.

Want to know more?

Read Introducing the PdfFont class which is chapter 1 in the iText 7: Building Blocks tutorial. Get the free ebook!