How to include external font in WPF application without installing it How to include external font in WPF application without installing it wpf wpf

How to include external font in WPF application without installing it


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


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>


The best answer to this question I found here

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

SOLUTIONIt 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