2013-06-06 38 views
0

我想通過使用chunk來加粗我的字體。但特別的是,我的標籤是在數據讀取器內的addcell下編寫的。當使用chunk時返回true iTextSharp

這是我嘗試我的DataReader中格式化我的標籤

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

這是聲明短語和字體類型:

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

這是正在顯示的內容:

enter image description here

但是在我嘗試格式化我的標籤,這是怎麼看起來像

enter image description here

這是我只是直接添加標籤到我table.AddCell

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

我不明白的問題,我不明白爲什麼你的代碼編譯。可以添加一個字符串在C#中的短語?這不應該工作,應該嗎? –

+0

@BrunoLowagie [Phrase.cs](http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/Phrase.cs#l283)有一個重載'public bool Add (字符串s)'這基本上增加了一個'新的塊(s,字體)。' – mkl

+0

好的,這是什麼問題?「我的標籤是在datareader內的addcell下面寫的」是什麼意思? –

回答

2

您通過

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

table.AddCell。的Phrase.Add這裏使用的過載被聲明爲

public virtual new bool Add(IElement element) 

(參見Phrase.cs

因此,phrase.Add(new Chunk("test:", normalFont))計算結果爲布爾值true,你有

true + dr[0].ToString() 

現在,布爾值被轉換爲string本身:

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

你的情況dr[0].ToString()似乎包含"admin"。所以:

"True" + "admin" 

從此以後:

"Trueadmin" 

而且因爲這string傳遞給table.AddCell,你得到你所看到的。

table cell with content "trueadmin"

相反,你可能想要做的線沿線的東西:

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

好的+1。由於「我的標籤寫在數據讀取器內的addcell下」,我不知道問題所在。 –

+0

非常感謝它!我將不勝感激,如果你知道如何解決我的這個問題http://stackoverflow.com/questions/16952561/retrieve-decoded-binary-image-from-sql-and-insert-into-pdf-via-itextsharp -Asp-NE –

相關問題