Nick K
Nick K

Reputation: 389

iTextSharp - Is it possible to set a different font color for the same cell and row?

I am using the iTextSharp.dll with the following code:

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

Can we set different font colors for title and description, but only using single cell (ie without creating a new table)?

Any help in this matter would be greatly appreciated.

Upvotes: 19

Views: 61488

Answers (4)

Andy Brown
Andy Brown

Reputation: 5522

The trick is to create phrases (NOT chunks) with different fonts and add these to a parent phrase. As far as I can tell if you create chunks with different fonts and add these to a phrase, all of the text in the final phrase displays with the same font.

Here's an example which works for me:

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

Upvotes: 0

Johnbosco Adam
Johnbosco Adam

Reputation: 91

For VB.net Use the following function

 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)

Upvotes: 0

Nalan Madheswaran
Nalan Madheswaran

Reputation: 10572

Try like this to set a different foreground color in a pdf cell:

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

Upvotes: 5

TimS
TimS

Reputation: 2113

What you want to do is create 2 Chunk objects, and then combine these into 1 Phrase which you will add to the cell.

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

Have a look at http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

Upvotes: 27

Related Questions