无法在iTextsharp中更改字体?

时间:2016-07-02 08:25:26

标签: c# asp.net .net itext

我正在使用iTextSharp生成表格并以pdf格式打印。 我正在尝试更改字体,但它实际上没有任何影响。

  Font tablefont = new Font();
            tablefont=FontFactory.GetFont(FontFactory.HELVETICA, 8,BaseColor.RED) ;     

            table.AddCell("Name :");
            PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds") {Font=tablefont}); 

1 个答案:

答案 0 :(得分:0)

如果您要开始一个新项目,我建议将iText 7用于iTextSharp,因为我们已经在该版本中引入了字体继承;见Chapter 1: Introducing the PdfFont class

如果你坚持使用iTextSharp 5.5.9,你应该阅读旧的教程,并像这样创建你的Phrase

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font tablefont = new Font(bfTimes, 8);
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds", tablefont));

或者像这样:

Font tablefont = new Font(FontFamily.TIMES_ROMAN, 8);
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds", tablefont));

或者像这样:

Font tablefont = FontFactory.GetFont("Times-Roman", 8);
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds", tablefont));

您可以在iText in Action手册的chapter 11: Choosing the right font中找到大量旧字体示例。该页面上显示的示例均为Java,但如果向下滚动,您将找到指向所有相应.cs文件的链接。

还有一个广泛的FAQ section about fonts,包含过去在StackOverflow上发布的问题的答案。

虽然不是完全重复的,但我通过引用How to use System Font in iTextSharp with VB.net

来结束了这个问题。

您正在使用C#,但以下行与您需要的内容非常相似:

document.Add(New Paragraph("Hello World, Arial.", font))

Paragraph类是您正在使用的Phrase类的子类。与Paragraph一样,您需要将font(在您的情况下为tablefont)作为构造函数的第二个参数传递。

我提到How to use System Font in iTextSharp with VB.net的第二个原因是:在回答这个问题时,您会获得有关如何使用计算机上可用字体的详细信息。