iText 7: How to build a paragraph mixing different fonts?

lpinho picture lpinho · Jun 22, 2016 · Viewed 11.1k times · Source

I've been using iText 7 for a few days to build pdf files, unfortunately, iText 7 is very different from iText 5 and the documentation is still very incomplete.

I'm trying to build a paragraph that mixes two fonts or two fonts style (example: have a bold text in the middle of a paragraph)

Using iText 5 this would be done using Chunks:

Font regular = new Font(FontFamily.HELVETICA, 12);
Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Phrase p = new Phrase("NAME: ", bold);
p.add(new Chunk(cc_cust_dob, regular));
PdfPCell cell = new PdfPCell(p);

Using iText 7, I still haven't found way to do this.

Has anyone tried to do this using the last version of iText?

Note: I'm using the csharp but java is also useful

Answer

Bruno Lowagie picture Bruno Lowagie · Jun 22, 2016

Please read the documentation, more specifically iText 7: building blocks "Chapter 1: Introducing the PdfFont class"

In that chapter, you'll discover that it's much easier to switch fonts when using iText 7, because you can work with default fonts and font sizes, you can define and reuse Style objects, and so on.

An example:

Style normal = new Style();
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
normal.setFont(font).setFontSize(14);
Style code = new Style();
PdfFont monospace = PdfFontFactory.createFont(FontConstants.COURIER);
code.setFont(monospace).setFontColor(Color.RED)
    .setBackgroundColor(Color.LIGHT_GRAY);
Paragraph p = new Paragraph();
p.add(new Text("The Strange Case of ").addStyle(normal));
p.add(new Text("Dr. Jekyll").addStyle(code));
p.add(new Text(" and ").addStyle(normal));
p.add(new Text("Mr. Hyde").addStyle(code));
p.add(new Text(".").addStyle(normal));
document.add(p);

First we define a Style that we call normal and that uses 14 pt Times-Roman. Then we define a Style that we call code and that uses 12 pt Courier in Red with a gray background.

Then we compose a Paragraph using Text objects that use these styles.

Note that you can chain add() comments, as is done in this example:

Text title1 = new Text("The Strange Case of ").setFontSize(12);
Text title2 = new Text("Dr. Jekyll and Mr. Hyde").setFontSize(16);
Text author = new Text("Robert Louis Stevenson");
Paragraph p = new Paragraph().setFontSize(8)
    .add(title1).add(title2).add(" by ").add(author);
document.add(p);

We set the font size of the newly created Paragraph to 8 pt. This font size will be inherited by all the objects that are added to the Paragraph, unless the objects override that default size. This is the case for title1 for which we defined a font size of 12 pt and for title2 for which we defined a font size of 16 pt. The content added as a String (" by ") and the content added as a Text object for which no font size was defined inherit the font size 8 pt from the Paragraph to which they are added.

This is a copy/paste from the official tutorial. I hope this is sufficient for StackOverflow where "link-only" answers aren't allowed. This "no link-only answers rule" shouldn't lead to copy/pasting a full chapter of a manual...