2012-12-13 9 views
5

iText5(Java)を使用して、漢字を含むPDFを作成しています。だから、私はFontSelectorを処理して文字列を処理しています。これはうまくいきます。FontSelectorを使用するときのフォントの色とサイズの変更

今の問題は、2列

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

がある場合、私はFont Color = Graystr2size = 25のに対し、str1魔女Font Color = Bluesize = 10を記述する必要があるということです。

FontSelectorを使用してこれを達成する方法を理解できません。

何か助けていただければ幸いです。

答えて

8

これは簡単です。ここでは、ブルー・タイムズローマンテキストと赤で中国語のテキストを追加するコードスニペットを持っている:あなたのケースで

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

あなたは2 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 
+0

恐ろしい表形式のレポートのデータのためのヘッダの14と10を使用して私の場合は、別の方法でそれを行うことができます。ありがとうございました。 – Neela

0

あなたは私が

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 
関連する問題