使用 ColumnText 设置字体
set font with 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
有人可以给我建议吗?谢谢
您将 Paragraph 的实例传递给第二个 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);
我知道这个问题已经有了(正确的)答案,但我想补充一点,原代码中有很多地方是错误的。我会 copy/paste 代码并解释为什么它们是错误的。
// 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();
我尝试使用 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
有人可以给我建议吗?谢谢
您将 Paragraph 的实例传递给第二个 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);
我知道这个问题已经有了(正确的)答案,但我想补充一点,原代码中有很多地方是错误的。我会 copy/paste 代码并解释为什么它们是错误的。
// 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();