How to use multiple fonts in a single cell?

Category: 
Tags: fontstablesiText 7

I'm making a windows form for a friend who delivers packages. I want to transfer his current paper form, into a PDF with the iTextSharp library.

What I need:

I want the table to have a little headline, "Company name" for example, where the text should be a little smaller than the text input from the windows form. Currently I'm using cells and was wondering if I can use 2 different font sizes within the same cell?

What I have:

table.AddCell("Static headline" + Chunk.NEWLINE + richTextBox1.Text);
What I "want":

var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 9);
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
table.AddCell("Static headline", boldFont + Chunk.NEWLINE + richTextBox1.Text, normalFont);
Posted on StackOverflow on Feb 13, 2014 by Frederik Kiel

You're passing a String and a Font to the AddCell() method. That's not going to work. You need the AddCell() method that takes a Paragraph object as parameter. In iText 7 Paragraph consists of different Text snippets that can have different fonts and sizes:

Cell cell = new Cell();
 
Paragraph p = new Paragraph();
Text t1 = new Text("Some bold text, ");
t1.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD));
t1.setFontSize(9);
p.add(t1);
 
Text t2 = new Text("some normal text");
t2.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA));
t2.setFontSize(12);
p.add(t2);
 
cell.add(p);

If you want an iText for C# example, you'll discover that it is very easy to port the Java to C# as the terminology is identical.

Click this link if you want to see how to answer this question in iText 5.