Convert All Used Fonts in Presentation to Embedded without Having These Fonts on a Local Machine

Hi!
I have a .pptx file with custom fonts. I want no make all used fonts as embedded programmatically. These fonts are not installed on my machine and I haven’t any details about it (names, etc.)
So I want to do the same thing like this option in the PowerPoint on my screenshot
image.png (29.4 KB)

I’ve even found the code which probably should do that:

private static void EmbedFonts(Presentation presentation)
{
    var allFonts = presentation.FontsManager.GetFonts().ToList();
    var embeddedFonts = presentation.FontsManager.GetEmbeddedFonts().ToList();

    foreach (var font in allFonts)
    {
        if (embeddedFonts.All(x => x.FontName != font.FontName))
        {
            embeddedFonts.Add(font);
            try
            {
                presentation.FontsManager.AddEmbeddedFont(font, EmbedFontCharacters.OnlyUsed);
            }
            catch (Exception)
            {
                Console.WriteLine($"Font is not found: {font.FontName}");
            }
        }
    }
}

However, if some font in this cycle is not installed on my machine I’ve get the InvalidOperationException with text “font is not found” from FontsManager.AddEmbeddedFont(…)

So it looks like FontsManager.AddEmbeddedFont <-- doesn’t work if a font is not installed locally. Is it correct? For me it’s the key point because I don’t know any information about my custom fonts and I’m not be able to download/install it. I want to take these fonts from presentation itself like PowerPoint magically do that and then make it embedded.

PowerPoint do exactly what I need but I wonder to do the same thing programmatically for my clients. Please, provide any information about this feature, thx

@h0las,
Thank you for describing the issue.

PowerPoint can also embed used fonts in a presentation only if the fonts are readable from the local machine. If a custom font is not embedded in the presentation, PowerPoint will attempt to read it from the operating system. If the font is missing when saving the presentation, an error message will appear. For example, the following presentation uses a custom font, and if you try to enable the Embed fonts in the file option and save the presentation, the error message will appear (see the screenshot):
example.zip (103.0 KB)

If a font file is not available, it cannot be embedded into a presentation using either PowerPoint or Aspose.Slides.

Useful articles: PowerPoint Fonts

1 Like

@andrey.potapov Hmm… Yes, you are absolutely right, my fault. Thank you for detailed answer! In my case custom fonts were installed on my VM and I hadn’t expected it. So that’s why Embed fonts option works there

@h0las,
We are glad you have sorted out this issue. Thank you for using Aspose.Slides.

1 Like