C# PrivateFontCollection AddFontFile from relative path

485 views Asked by At

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?

1

There are 1 answers

0
Alen Šimunic On BEST ANSWER

I have found a solution using MapPath:

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