2012-12-13 161 views
5

我正在使用iText5(Java)編寫可能包含中文字符的PDF。所以我使用FontSelector來處理字符串,這工作正常。使用FontSelector時更改字體顏色和大小

現在的問題是,如果有2串

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

我需要寫str1Font Color = Bluesize = 10,而str2Font Color = Graysize = 25

我無法弄清楚如何使用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); 

你的情況,你需要兩個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

真棒。謝謝。 – Neela

0

你能做到這一點,在我的情況下,另一種方式我在表格報告使用的數據爲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