Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the font color and size when using FontSelector

I am using iText5 (Java) to write a PDF which may contain Chinese characters. So I am using FontSelector 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.

Any help is appreciated.

like image 962
Anup Mayank Avatar asked Dec 13 '12 09:12

Anup Mayank


People also ask

How do you change the font color and size?

You can use a <basefont> tag to set all of your text to the same size, face, and color. The font tag is having three attributes called size, color, and face to customize your fonts. To change any of the font attributes at any time within your webpage, simply use the <font> tag.

How do you change font color in HTML?

You can change the color and size of your text right inside its tag with the color and font-size properties. This is known as inline CSS. You do it with the style attribute in HTML.

How do you change the font color style?

On the Home tab, in the Font group, choose the arrow next to Font Color, and then select a color. You can also use the formatting options on the Mini toolbar to quickly format text.

How do I change the font size in HTML?

In HTML, you can change the size of text with the <font> tag using the size attribute. The size attribute specifies how large a font will be displayed in either relative or absolute terms. Close the <font> tag with </font> to return to a normal text size.


1 Answers

That's easy. 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);
f1.setColor(BaseColor.BLUE);
Font f2 = FontFactory.getFont("MSung-Light",
        "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED);
f2.setColor(BaseColor.RED);
selector.addFont(f1);
selector.addFont(f2);
Phrase ph = selector.process(TEXT);

In your case you need two FontSelectors.

FontSelector selector1 = new FontSelector();
Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
f1.setColor(BaseColor.BLUE);
selector1.addFont(f1);
Phrase ph = selector1.process(str1);//First one

FontSelector selector2 = new FontSelector();
Font f2 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
f2.setColor(BaseColor.GRAY);
selector2.addFont(f2);
Phrase ph = selector2.process(str2);//Second one
like image 75
Bruno Lowagie Avatar answered Sep 28 '22 01:09

Bruno Lowagie