2015-06-25 29 views
0

我嘗試用ColumnText設置字體,但它不工作。用ColumnText設置字體

PdfContentByte cb = writer.DirectContent; 

BaseFont title = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED); 

cb.SetFontAndSize(title, 10); //10 is the font size 
string text = "this is sample of long long long paragraph.."; 

ColumnText column1 = new ColumnText(cb); 

column1.SetSimpleColumn(255, 145, 600, 100);   
Paragraph p; 
p = new Paragraph(new Paragraph(text)); 
column1.Go(); 

我想這個代碼,它不工作,以及:

p = new Paragraph(new Paragraph(text, cb.SetFontAndSize(title, 10))); 

錯誤消息:The best overloaded method match for 'iTextSharp.text.Paragraph.Paragraph(string, iTextSharp.text.Font)' has some invalid arguments

有人能指點我?謝謝

回答

2

您將段落的實例傳遞給第二個段落的構造函數。

嘗試:

BaseFont title = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED); 
Font titleFont = new Font(title, 10, Font.BOLD, Color.BLACK); 
p = new Paragraph(text, titleFont); 
+0

感謝您的答覆,但它不工作爲好,同樣的錯誤信息 – user3431239

1

我知道這個問題已經有一個(正確)的答案,但我想補充的是,在最初的代碼中的許多事情都是錯誤的。我會複製/粘貼代碼並解釋他們爲什麼錯了。

// This is right: if you want to use ColumnText, you need a PdfContentByte 
PdfContentByte cb = writer.DirectContent; 
// This may not be necessary if you merely need Helvetica Bold in a Paragraph, but it's not incorrect. 
BaseFont title = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED); 
// YOU DON'T NEED THE FOLLOWING LINE. PLEASE REMOVE IT! 
cb.SetFontAndSize(title, 10); //10 is the font size 
// OK, you're just defining a string 
string text = "this is sample of long long long paragraph.."; 
// OK, you're defining a ColumnText object and defining the rectangle 
ColumnText column1 = new ColumnText(cb); 
column1.SetSimpleColumn(255, 145, 600, 100);   
// OK, you're defining a paragraph 
Paragraph p; 
// This is strange: why do you nest paragraphs? 
// Why don't you use the font? 
p = new Paragraph(new Paragraph(text)); 
// You are forgetting a line here: where do you add the paragraph to the column? 
// Nothing will happen here: 
column1.Go(); 

這是我怎麼會重寫代碼:

PdfContentByte cb = writer.DirectContent; 
ColumnText column1 = new ColumnText(cb); 
column1.SetSimpleColumn(255, 145, 600, 100); 
Font font = new Font(FontFamily.HELVETICA_BOLD); 
string text = "this is sample of long long long paragraph.."; 
Paragraph p = new Paragraph(text, font); 
column1.AddElement(p); 
column1.Go();