我尝试使用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
有人可以建议我吗?感谢
答案 0 :(得分:3)
将Paragraph的实例传递给第二段的构造函数。
尝试:
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);
答案 1 :(得分: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();