0

I'm trying to bold my font by using chunk. But the special thing is that my label is written under the addcell within a datareader.

This is how i attempt to format my label within my datareader

table.AddCell(phrase.Add(new Chunk("test:", normalFont)) + dr[0].ToString());

This is the declaration to phrase and font type:

var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
var phrase = new Phrase();

And this is what is being displayed:

enter image description here

However before i attempt to format my label this is how it will look like

enter image description here

This is where i just directly add a label into my table.AddCell

table.AddCell(dr[0].ToString());
Mark
  • 8,046
  • 15
  • 48
  • 78
Bryan
  • 8,488
  • 14
  • 52
  • 78
  • I don't understand the question, and I don't understand why your code compile. Can one add a string to a Phrase in C#? that shouldn't work, should it? – Bruno Lowagie Jun 06 '13 at 06:47
  • @BrunoLowagie [Phrase.cs](http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/Phrase.cs#l283) has an overload `public bool Add(String s)` which essentially adds a `new Chunk(s, font).` – mkl Jun 06 '13 at 08:21
  • OK, and what is the question? What does "my label is written under the addcell within a datareader" mean? – Bruno Lowagie Jun 06 '13 at 08:28
  • @BrunoLowagie what i meant was the words such as "test" was meant to be used as a label to say that the value displayed out for "test" is admin. Written under the addcell because i wrote the "label" which is "test" for example is in a addcell and within a datareader method. I used the datareader to read the data from the SQL server. – Bryan Jun 07 '13 at 01:02

1 Answers1

2

You pass

phrase.Add(new Chunk("test:", normalFont)) + dr[0].ToString()

to table.AddCell. The overload of Phrase.Add used here is declared as

public virtual new bool Add(IElement element)

(cf. Phrase.cs)

Thus, phrase.Add(new Chunk("test:", normalFont)) evaluates to the boolean value true and you have

true + dr[0].ToString()

Now the boolean is converted to a string itself:

"True" + dr[0].ToString()

In your case dr[0].ToString() seems to contain "admin". So:

"True" + "admin"

Henceforth:

"Trueadmin"

And as this string is passed to table.AddCell, you get what you see.

table cell with content "trueadmin"

Instead you might want to do something along the lines of:

phrase.Add(new Chunk("test:", normalFont));
phrase.Add(dr[0].ToString());
table.AddCell(phrase);
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Good catch +1. I had no clue what the question was about due to the "my label is written under the addcell within a datareader". – Bruno Lowagie Jun 06 '13 at 08:30
  • Thanks a lot it works! i would appreciate if any of you knows how to solve this question of mine http://stackoverflow.com/questions/16952561/retrieve-decoded-binary-image-from-sql-and-insert-into-pdf-via-itextsharp-asp-ne – Bryan Jun 07 '13 at 02:23