2012-06-15 18 views
10

Aşağıdaki kod ile iTextSharp.dll kullanıyorum:iTextSharp - Aynı hücre ve satır için farklı bir yazı tipi rengi ayarlamak mümkün mü?

var Title = "This is title"; 
var Description = "This is description"; 

Innertable.AddCell(new PdfPCell(new Phrase(string.Format("{0} {1}", Title, Description.Trim()), listTextFont)) { BackgroundColor = new BaseColor(233, 244, 249), BorderWidth = 0, PaddingTop = 4, PaddingLeft = -240, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_LEFT }); 

biz başlık ve açıklama için farklı yazı tipi renkleri ayarlayabilir miyim, fakat sadece tek bir hücre (yani yeni bir tablo oluşturmadan) kullanarak?

Bu konuyla ilgili herhangi bir yardım çok takdir edilecektir.

cevap

16

Yapmak istediğiniz şey, 2 Chunk nesneleri oluşturmak ve sonra bunları hücreye ekleyeceğiniz 1 Phrase içine birleştirmektir.

var blackListTextFont = FontFactory.GetFont("Arial", 28, Color.BLACK); 
var redListTextFont = FontFactory.GetFont("Arial", 28, Color.RED); 

var titleChunk = new Chunk("Title", blackListTextFont); 
var descriptionChunk = new Chunk("Description", redListTextFont); 

var phrase = new Phrase(titleChunk); 
phrase.Add(descriptionChunk); 

table.AddCell(new PdfPCell(phrase)); 

pdf hücrede farklı bir ön plan rengini ayarlamak için böyle deneyin http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

+0

Çözüm Yukarıdaki iyi görünüyor, ancak çalışmıyor:

İşte Benim için çalışan bir örnek. Bu örnekte, her iki font da aynı çıktı: var titleChunk = new Chunk ("UPRN:", _fntHeading9); var descriptionChunk = new Chunk (keyPropertyId.ToString(), _fntNormal9); var phrase = new Cümle (titleChunk); phrase.Add (descriptionChunk); table.AddCell (yeni PdfPCell (ifade)); –

+0

Kod kesinlikle çalışır, belki de yazı tipi nesneleri aynı yazı tipine işaret eder? Ya da oyunda başka bir sorun var. – TimS

+4

Kesinlikle işe yaramadı. Yazı tipi nesneleri farklıdır. Biri Kalın, diğeri Normaldir. Bu kod çalıştı: public static Yazı tipi _fntNormal9 = FontFactory.GetFont ("Arial", 9, Font.NORMAL); public static Yazı tipi _fntHeading9 = FontFactory.GetFont ("Arial", 9, Font.BOLD); var phrase = new Phrase(); phrase.Add (new Phrase ("UPRN:", _fntHeading9)); phrase.Add (new Phrase (keyPropertyId.ToString(), _fntNormal9)); table.AddCell (yeni PdfPCell (ifade)); –

3

göz at: VB.net Kullanımı İçin

var FontColour = new BaseColor(35, 31, 32); 
var Calibri8 = FontFactory.GetFont("Calibri", 8, FontColour); 

PdfPCell R3C2 = new PdfPCell(new Paragraph("Hello", Calibri8)); 
R3C2.BorderWidth = 0f; 
R3C2.HorizontalAlignment = PdfPCell.ALIGN_RIGHT; 
table.AddCell(R3C2); 
0

aşağıdaki işlevi

Public Function CreateFont(size As Integer, Optional style As Integer = iTextSharp.text.Font.BOLD) As iTextSharp.text.Font 
     Dim FontColour = New BaseColor(193, 36, 67) 'Color code 
     Return New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, size, style, FontColour) 
End Function 


Dim p As New Paragraph 
p.Add(New Paragraph("Your Sentence Here", CreateFont(12, iTextSharp.text.Font.BOLDITALIC))) 
pdfDoc.Add(p) 
+0

güzel. Teşekkürler !! –

0

Buradaki hile, farklı yazı tipleriyle (NOT chunks) sözcükler oluşturmak ve bunları pa dosyasına eklemektir. kira ifade. Farklı yazı tiplerine sahip parçalar oluşturup bunları bir ifadeye ekleyip eklemediğinizi anlayabildiğim kadarıyla, son ifadedeki tüm metinler aynı yazı tipiyle görüntülenir.

// create the font we'll use 
var fNormal = FontFactory.GetFont("Helvetica", 10f); 
fNormal.SetColor(0, 0, 0); 

// add phrase containing link 
var pFooter = new Phrase(); 

// add phrase to this containing text only 
var footerStart = new Phrase("Visit "); 
footerStart.Font = fNormal; 
pFooter.Add(footerStart); 

// now create anchor and add with different font 
string wolSiteUrl = "http://www.whateveryoulike.com"; 
Anchor wolWebSiteLink = new Anchor(wolSiteUrl, fNormal); 
wolWebSiteLink.Reference = wolSiteUrl; 
wolWebSiteLink.Font = fNormal; 
wolWebSiteLink.Font.SetColor(242, 132, 0); 
pFooter.Add(wolWebSiteLink); 

// add text to go after this link 
var footerEnd = new Phrase(" to view these results online."); 
footerEnd.Font = fNormal; 
pFooter.Add(footerEnd); 
var paraFooter = new Paragraph(pFooter); 

// add the phrase we've built up containing lots of little phrases to document 
// (assume you have one of these ...) 
doc.Add(paraFooter); 
İlgili konular