How to include external font in WPF application without installing it

83,122

Solution 1

I use such XAML code:

<Style x:Key="Hatten">
        <Setter Property="TextElement.FontFamily" Value="Resources/#HATTEN" />
</Style>

#HATTEN - reference to hatten.tft in Resources.

Using the Style:

 <TextBlock x:Name="lblTitle" Style="{DynamicResource Hatten}" FontSize="72"></TextBlock>

Solution 2

This are two ways of doing this. One way is to package the fonts inside the application. The other way is to have the fonts in an folder. The difference is mostly the URI you need to load the files.

Package with Application

  1. Add a /Fonts folder to your solution.

  2. Add the True Type Fonts (*.ttf) files to that folder

  3. Include the files to the project

  4. Select the fonts and add them to the solution

  5. Set BuildAction: Resource and Copy To Output Directory: Do not copy. Your .csproj file should now should have a section like this one:

      <ItemGroup>
       <Resource Include="Fonts\NotoSans-Bold.ttf" />
       <Resource Include="Fonts\NotoSans-BoldItalic.ttf" />
       <Resource Include="Fonts\NotoSans-Italic.ttf" />
       <Resource Include="Fonts\NotoSans-Regular.ttf" />
       <Resource Include="Fonts\NotoSansSymbols-Regular.ttf" />
     </ItemGroup>
    
  6. In App.xaml add <FontFamily> Resources. It should look like in the following code sample. Note that the URI doesn't contain the filename when packing with the application.

     <Applicaton ...>
     <Application.Resources>
         <FontFamily x:Key="NotoSans">pack://application:,,,/Fonts/#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansSymbols">pack://application:,,,/Fonts/#Noto Sans Symbols</FontFamily>
     </Application.Resources>
     </Application>
    
  7. Apply your Fonts like this:

     <TextBlock x:Name="myTextBlock" Text="foobar" FontFamily="{StaticResource NotoSans}" 
                FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
    
  8. You can also set the font imperatively:

     myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#Noto Sans");
    

Copy to Output Directory

  1. Add a /Fonts folder to your solution.

  2. Add the True Type Fonts (*.ttf) files to that order

  3. Include the files to the project

  4. Select the fonts and add them to the solution

  5. Set BuildAction: Content and Copy To Output Directory: Copy if newer or Copy always. Your .csproj file should now should have a section like this one:

      <ItemGroup>
       <Content Include="Fonts\NotoSans-Bold.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="Fonts\NotoSans-BoldItalic.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="Fonts\NotoSans-Italic.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="Fonts\NotoSans-Regular.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="Fonts\NotoSansSymbols-Regular.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
     </ItemGroup>
    
  6. In App.xaml add <FontFamily> Resources. It should look like in the following code sample.

     <Applicaton ...>
     <Application.Resources>
         <FontFamily x:Key="NotoSansRegular">./Fonts/NotoSans-Regular.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansItalic">./Fonts/NotoSans-Italic.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansBold">./Fonts/NotoSans-Bold.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansBoldItalic">./Fonts/NotoSans-BoldItalic.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansSymbols">./Fonts/NotoSans-Regular.ttf#Noto Sans Symbols</FontFamily>
     </Application.Resources>
     </Application>
    
  7. Apply your Fonts like this:

     <TextBlock Text="foobar" FontFamily="{StaticResource NotoSansRegular}" 
                FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
    

References

Solution 3

The best answer to this question I found here

http://geekswithblogs.net/Martinez/archive/2010/01/29/custom-font-in-wpf-application.aspx

SOLUTION It involves using even more wicked string than before but works as expected:

<Label FontFamily="pack://application:,,,/Folder1/#Katana Sans">Text</Label> 

Where is Folder1 is a folder of your project where you keep a TTF file. Three notes at the end:

  1. ‘Katana Sans’ is the name of the font, not the name of the file. This is significant difference. To get the name of the font simply click the file twice. Note that spaces are included without any changes in this string.

  2. Remember to put the hash sign ‘#’ in front of font name. It will not work otherwise.

  3. Custom font may also be added to the project with ‘Build Action’ set to ‘Content’. This is not recommended approach however and for the sake of simplicity I ignored this possibility.

Some extra links

https://msdn.microsoft.com/en-us/library/ms753303(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/cc296385.aspx

Solution 4

The easiest way to include external fonts is to

Step 1. Create the folder Fonts And add your fonts into it.

enter image description here

Step 2. Set Build action to content and Copy to Output Directory to Copy always.

enter image description here

Step 3. Build the Solution to update it with the Font directory.

Step 4. Use the font in your elements using FontFamily="Fonts/#font name"

enter image description here

All Done!

Thanks to cscience !

Solution 5

I did not found any answer for exactly that. But I found a solution that I did not saw on Internet.

I followed a recommendation of doing a folder and marking all the files inside as Resources. But I needed to enumerate them, and that was my main problem, because I need to load all to my screen without recording the name somewhere. I just want to simple drop another font to that folder and list it.

I found this as a solution to list all the files inside my resources/fonts folder

Fonts.GetFontFamilies(new Uri("pack://application:,,,/resources/fonts/#"))

I expect it to help you organize your fonts.

Share:
83,122
Shebin
Author by

Shebin

.Net Developer

Updated on July 05, 2022

Comments

  • Shebin
    Shebin almost 2 years

    How to include external font in WPF application without installing it

    I tried this code

      System.Drawing.Text.PrivateFontCollection privateFonts = new    System.Drawing.Text.PrivateFontCollection();
      privateFonts.AddFontFile("C:\\Documents and Settings\\somefont.ttf");
      System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
      this.label1.Font = font;
    

    It working correctly in Windows Form Application but not in WPF.

  • Adam Zuckerman
    Adam Zuckerman about 10 years
    Please add a link to the reference page for this API.
  • Jack Miller
    Jack Miller over 7 years
    The code snippet <Style> is incomplete/misleading. Better follow MovGP0's answer. It is basically the same but more detailed. OTF works, too.
  • Jack Miller
    Jack Miller over 7 years
    Please clarify where #names are coming from and how they are constructed. Just file name without extensions preceded by #? Otherwise: great, detailed answer!
  • MovGP0
    MovGP0 over 7 years
    the string after the hash is the name of the font. unfortunately i don't really know how to get them except opening the font file in preview or installing it and using the font selection in an text editor. there might be a better way of doing this...
  • Kyle Delaney
    Kyle Delaney about 7 years
    The package option does not work for me in VS2017. I followed your instructions to the letter.
  • lindexi
    lindexi almost 7 years
    Can you pick a font file and use it?
  • lindexi
    lindexi almost 7 years
    Can GetFontFamilies pick the font in the other folder?
  • Vincent
    Vincent almost 7 years
    Step "6" in "Package with Application" works in VS2015, but... at runtime application shows just empty squares! I removed "FontFamily" resource and specified font directly in style's Setter: <Setter Property="FontFamily" Value="./media/#FontAwesome"/>
  • Gabe Halsmer
    Gabe Halsmer about 6 years
    Important: "‘Katana’ is the name of the font, not the name of the file. This is significant difference. To get the name of the font simply click the file twice."
  • NearHuscarl
    NearHuscarl almost 6 years
    @KyleDelaney I followed the steps in "Package with Application" in VS2017 but it didnt work. But after I changed build action to Content and Copy to Output Directory to Copy if newer, it works fantastically.
  • nietras
    nietras almost 6 years
    This suffers from the WPF memory leak issue as detailed in stackoverflow.com/questions/31452443/… as do all the other solutions given here it seems.
  • nietras
    nietras almost 6 years
    I tried doing this with AddFontMemResourceEx but seem to have issues with as can be seen in stackoverflow.com/questions/50964801/…
  • Rachel Martin
    Rachel Martin over 5 years
    @JackMiller, I know it's been a while, but I had the same question. You can get the #names by doing the following: Right-click the font in Windows explorer. Go to Properties -> Details -> Title.
  • ataraxia
    ataraxia over 5 years
    Does the memory leak still occur when using the second method of copying to the output directory?
  • kayleeFrye_onDeck
    kayleeFrye_onDeck about 5 years
    @KyleDelaney In VS2017 you can just add a Fonts folder to your source as Resources and you can then choose the font with the drop-down menu box, looking something like this: FontFamily="/AppName;component/Fonts/#Font Name"
  • The Muffin Man
    The Muffin Man almost 5 years
    FYI that cryptic #NotoSans identifier is not driven by the file name. You need to double click on the file in Windows which opens the preview window for the font. There will be a label that says "Font name: NotoSans", it's that font name you want to use when referencing in WPF.
  • JvdBerg
    JvdBerg almost 5 years
    in VS2019 and dotnet core 3 I used <FontFamily x:Key="FontAwesome">/MyResourceClassLib;component/Fonts/#Fon‌​t Awesome 5 Free Solid</FontFamily> in step 6
  • Kamil
    Kamil almost 5 years
    Thank you so much for this explanation!
  • rollsch
    rollsch almost 5 years
    How does it know what the filename is though? Surely the file name needs to go somewhere? This solution doesn't work for me.
  • rollsch
    rollsch almost 5 years
    This was extremely useful.
  • Axel Samyn
    Axel Samyn over 4 years
    It worked for me, but only in designer :/ Don't forget that the font's name is case sensitive !
  • Cole Tobin
    Cole Tobin over 4 years
    @rolls It will look through the folder for font files and pick the one that matches that name.
  • Mour_Ka
    Mour_Ka almost 4 years
    Your solution for setting the font properties to Build Action: Resource solve it for me. Only you need this in <FontFamily x:Key="myFont">pack://application:,,,/AssemlyName;component/‌​Resources/Fonts/#IRA‌​NSansWeb </FontFamily>
  • Jonathan Tuzman
    Jonathan Tuzman almost 4 years
    What made the difference for me was including spaces! My Nunito Sans is used as #Nunito Sans not #NunitoSans or #Nunito_Sans or anything else!
  • TobiasW
    TobiasW over 3 years
    This isn't an answer to the question right? None of the answers here explains how to include EXTERNAL fonts in the project. Does that simply mean, that it's not possible to load a font dynamically during runtime into the project?
  • Rye bread
    Rye bread about 3 years
    There is an error: ./Fonts/NotoSans-Regular.tts should be: ./Fonts/NotoSans-Regular.ttf
  • MovGP0
    MovGP0 about 3 years
    @Rugbrød Thanks. I've fixed that in the code sample.