gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:52:33 24 4
gpt4 key购买 nike

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

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

String str1 = "Hello Test1";
String str2 = "Hello Test2";

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

我无法弄清楚如何使用 FontSelector 来实现这一点.

任何帮助表示赞赏。

最佳答案

这很容易。这里有一个代码片段,添加了蓝色的 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);

在您的情况下,您需要两个 FontSelector。
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

关于itext - 使用 FontSelector 时更改字体颜色和大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13857273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com