Return true when chunk is used iTextSharp

我与影子孤独终老i 提交于 2019-12-13 06:14:36

问题


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:

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

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

table.AddCell(dr[0].ToString());

回答1:


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.

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


来源:https://stackoverflow.com/questions/16952771/return-true-when-chunk-is-used-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!