Search code examples
c#model-view-controllerfontsprivatefontcollection

C# PrivateFontCollection AddFontFile from relative path


I have OpenSans-Light.ttf in the this folder from root: ~/fonts/open-sans/OpenSans-Light.ttf

PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile("~/fonts/open-sans/OpenSans-Light.ttf");

but I get "System.IO.FileNotFoundException: File not found"

How do I target that font file?


Solution

  • I have found a solution using MapPath:

    PrivateFontCollection privateFontCollection = new PrivateFontCollection();
    privateFontCollection.AddFontFile(HttpContext.Current.Server.MapPath("~/fonts/open-sans/OpenSans-Light.ttf"));