Custom PowerPoint Font in C#

Load Custom Fonts

Aspose.Slides allows you to load fonts that are rendered in presentations without having to install those fonts. The fonts are loaded from a custom directory.

  1. Create an instance of the FontsLoader Class and call the LoadExternalFonts method.
  2. Load the presentation that will be rendered.
  3. Clear the cache in the FontsLoader Class.

This C# code demonstrates the font loading process:

// The path to the documents directory
string dataDir = "C:\\";

// folders to seek fonts
String[] folders = new String[] { dataDir };

// Loads the custom font directory fonts
FontsLoader.LoadExternalFonts(folders);

// Do some work and perform presentation/slide rendering
using (Presentation presentation = new Presentation(dataDir + "DefaultFonts.pptx"))
    presentation.Save(dataDir + "NewFonts_out.pptx", SaveFormat.Pptx);

// Clears the font cache
FontsLoader.ClearCache();

Get Custom Fonts Folder

Aspose.Slides provides the GetFontFolders method to allow you to find font folders. This method returns folders added through the LoadExternalFonts method and system font folders.

This C# code shows you how to use GetFontFolders:

// This line outputs the folders that are checked for font files.
// Those are folders added through the LoadExternalFonts method and system font folders.
string[] fontFolders = FontsLoader.GetFontFolders();

Specify Custom Fonts Used With Presentation

Aspose.Slides provides the DocumentLevelFontSources property to allow you to specify external fonts that will be used with the presentation.

This C# code shows you how to use the DocumentLevelFontSources property:

byte[] memoryFont1 = File.ReadAllBytes("customfonts\\CustomFont1.ttf");
byte[] memoryFont2 = File.ReadAllBytes("customfonts\\CustomFont2.ttf");

LoadOptions loadOptions = new LoadOptions();
loadOptions.DocumentLevelFontSources.FontFolders = new string[] { "assets\\fonts", "global\\fonts" };
loadOptions.DocumentLevelFontSources.MemoryFonts = new byte[][] { memoryFont1, memoryFont2 };
using (IPresentation presentation = new Presentation("MyPresentation.pptx", loadOptions))
{
    // Work with the presentation
    // CustomFont1, CustomFont2, and fonts from assets\fonts & global\fonts folders and their subfolders are available to the presentation
}

Manage Fonts Externally

Aspose.Slides provides the LoadExternalFont(byte[] data) method to allow you to load external fonts from binary data.

This C# code demonstrates the byte array font loading process:

FontsLoader.LoadExternalFont(File.ReadAllBytes("ARIALN.TTF"));
FontsLoader.LoadExternalFont(File.ReadAllBytes("ARIALNBI.TTF"));
FontsLoader.LoadExternalFont(File.ReadAllBytes("ARIALNI.TTF"));

try
{
    using (Presentation pres = new Presentation(""))
    {
        // external font loaded during the presentation lifetime
    }
}
finally
{
    FontsLoader.ClearCache();
}