How to use multiple fonts in a single cell?

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 Phrase object or a PdfPCell object as parameter.

A Phrase is an object that consists of different Chunks, and the different Chunks can have different font sizes. For instance:

Phrase phrase = new Phrase();
phrase.Add(
    new Chunk("Some BOLD text",  new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD))
);
phrase.Add(new Chunk(", some normal text", new Font()));
table.AddCell(phrase);

A PdfPCell is an object to which you can add different objects, such as Phrases, Paragraphs, Images,...

PdfPCell cell = new PdfPCell();
cell.AddElement(new Paragraph("Hello"));
cell.AddElement(list);
cell.AddElement(image);

In this snippet list is of type List and image is of type Image.

The first snippet uses text mode; the second snippet uses composite mode. Cells behave very differently depending on the mode you use.

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



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now