使用FontSelector时更改字体颜色和大小

use*_*499 5 itext font-size

我正在使用iText5(Java)编写可能包含中文字符的PDF.所以我FontSelector用来处理String,这很好用.

现在的问题是,如果有2个字符串

String str1 = "Hello Test1";
String str2 = "Hello Test2";
Run Code Online (Sandbox Code Playgroud)

我需要写str1女巫Font Color = Bluesize = 10,而str2Font Color = Graysize = 25.

我无法弄清楚如何使用它FontSelector.

任何帮助表示赞赏.

Bru*_*gie 8

这很简单.这里有一个代码片段,用蓝色添加Times Roman文本,用红色添加中文文本:

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);
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您需要两个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
Run Code Online (Sandbox Code Playgroud)