使用FontSelector时更改字体颜色和大小 [英] Changing the font color and size when using FontSelector
问题描述
我正在使用 iText5
(Java)来编写可能包含中文字符的PDF。所以我使用 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.
现在的问题是,如果有的话2个字符串
Now the problem is that if there are 2 strings
String str1 = "Hello Test1";
String str2 = "Hello Test2";
我需要写 str1
witch 字体颜色=蓝色
和尺寸= 10
,而 str2
带字体颜色=灰色
和尺寸= 25
。
I need to write str1
witch Font Color = Blue
and size = 10
, whereas str2
with Font Color = Gray
and size = 25
.
I我无法弄清楚如何使用 FontSelector
来实现这一点。
I am not able to figure out how to achieve this using FontSelector
.
感谢任何帮助。
推荐答案
这很容易。这里有一个代码片段,用蓝色添加Times Roman文本,用红色添加中文文本:
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);
在您的情况下,您需要两个FontSelectors。
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
这篇关于使用FontSelector时更改字体颜色和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!