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 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.



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