Using Fonts in System with iTextSharp

后端 未结 4 813
粉色の甜心
粉色の甜心 2020-12-16 16:45

I want to use iTextSharp to write some text. I\'m using this method:

var font = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED)         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 16:58

    I ended up combining the 2 answers here into this method:

    public static Font GetFont(string fontName, string filename)
    {
        if (!FontFactory.IsRegistered(fontName))
        {
            var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\" + filename;
            FontFactory.Register(fontPath);
        }
        return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    }
    

    Which I then use in my code like so:

    writer.DirectContent.SetFontAndSize(GetFont("Franklin Gothic Medium Cond", "FRAMDCN.TTF").BaseFont, 24f);
    

    On Windows you can find out the font's file name from the font's property sheet:

    I also found that you have to use the font's exact name on the Details tab:

提交回复
热议问题