Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Itext 使用FontSelector时更改字体颜色和大小_Itext_Font Size - Fatal编程技术网

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

Itext 使用FontSelector时更改字体颜色和大小,itext,font-size,Itext,Font Size,我正在使用iText5(Java)编写一个可能包含中文字符的PDF。因此,我使用FontSelector处理字符串,效果很好 现在的问题是如果有两个字符串 String str1 = "Hello Test1"; String str2 = "Hello Test2"; 我需要编写str1开关Font Color=Blue和size=10,而str2使用Font Color=Gray和size=25 我不知道如何使用FontSelector实现这一点 非常感谢您的帮助。这很容易。这里有一个代码

我正在使用
iText5
(Java)编写一个可能包含中文字符的PDF。因此,我使用
FontSelector
处理字符串,效果很好

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

String str1 = "Hello Test1";
String str2 = "Hello Test2";
我需要编写
str1
开关
Font Color=Blue
size=10
,而
str2
使用
Font Color=Gray
size=25

我不知道如何使用
FontSelector
实现这一点


非常感谢您的帮助。

这很容易。这里有一个代码段,它添加了蓝色的泰晤士报罗马文本和红色的中文文本:

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 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

在我的例子中,你可以用另一种方式来做,我使用14作为标题,10作为表格报告中的数据

    private Font fHeader;
    private Font f1;

    BaseFont bf = BaseFont.createFont(Constants.Settings.ARIAL_FONT, BaseFont.IDENTITY_H, true);

     f1 = new Font(bf, 10);
     fHeader= new Font(bf,14);
     PdfPCell cell = new PdfPCell();

//for report header
     cell = new PdfPCell(new Phrase(reportKingdomData + "\n" + departmentData + " " + username + " \n  " + reportHeader + "    \n ", fHeader));

//and for background color
 cell .setBackgroundColor(new GrayColor(0.40f));//if 0.10f will be closer to black

令人惊叹的。非常感谢。