iText pdf library
Website search

How to change the font color and size when using FontSelector?

I am using iText (Java) to write PDF which may contain Chinese characters. So I am using font selector to process the String and this works fine.

Now the problem is that if there are 2 strings


String str1 = "Hello Test1";
String str2 = "Hello Test2";
I need to write str1 witch Font Color = Blue and size = 10, whereas str2 with Font Color = Gray and size = 25.

I am not able to figure out how to achieve this using FontSelector.

Posted on StackOverflow on Dec 13, 2012 by user1900499

Here you have a code snippet that adds the Times Roman text in Blue and the Chinese text in Red:

FontSelector selector = new FontSelector();
Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
Font f2 = FontFactory.getFont("MSung-Light",
        "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED);
Phrase ph = selector.process(TEXT);

In your case you need two FontSelectors.

FontSelector selector1 = new FontSelector();
Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
Phrase ph = selector.process(str1);
FontSelector selector2 = new FontSelector();
Font f2 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
Phrase ph = selector.process(str2);

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

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