Get Warning Callbacks for Font Substitution

Overview

Aspose.Slides for .NET allows you to receive warning callbacks for font substitution when a required font isn’t available on the machine during rendering. These callbacks help diagnose issues with missing or inaccessible fonts.

Enable Warning Callbacks

Aspose.Slides for .NET provides straightforward APIs for receiving warning callbacks when rendering presentation slides. Follow these steps to configure warning callbacks:

  1. Create a custom callback class that implements the IWarningCallback interface to handle warnings.
  2. Set the warning callback using option classes such as RenderingOptions, PdfOptions, HtmlOptions, and others.
  3. Load a presentation that uses a font not available on the target machine.
  4. Generate a slide thumbnail or export the presentation to observe the effect.

Custom Warning Callback Class:

class FontWarningHandler : IWarningCallback
{
    public ReturnAction Warning(IWarningInfo warning)
    {
        if (warning.WarningType == WarningType.DataLoss)
        {
            Console.WriteLine(warning.Description);
        }

        return ReturnAction.Continue;
    }
}

// Example output:
//
// Font will be substituted from XYZ to {Calibri,Cambria Math,MS Gothic,Gulim,Arial Unicode,SimSun,Segoe UI Symbol}}

Generate a Slide Thumbnail:

// Set up a warning callback to handle font-related warnings during slide rendering.
var options = new RenderingOptions();
options.WarningCallback = new FontWarningHandler();

// Load the presentation from the specified file path.
using var presentation = new Presentation("sample.pptx");

// Generate a thumbnail image for each slide in the presentation.
foreach (var slide in presentation.Slides)
{
    // Get the slide thumbnail image using the specified rendering options.
    using var image = slide.GetImage(options);
    // ...
}

Export to PDF Format:

// Set up a warning callback to handle font-related warnings during PDF export.
var options = new PdfOptions();
options.WarningCallback = new FontWarningHandler();

// Load the presentation from the specified file path.
using var presentation = new Presentation("sample.pptx");

// Export the presentation as PDF.
using var stream = new MemoryStream();
presentation.Save(stream, SaveFormat.Pdf, options);
// ...

Export to HTML Format:

// Set up a warning callback to handle font-related warnings during HTML export.
var options = new HtmlOptions();
options.WarningCallback = new FontWarningHandler();

// Load the presentation from the specified file path.
using var presentation = new Presentation("sample.pptx");

// Export the presentation in HTML format.
using var stream = new MemoryStream();
presentation.Save(stream, SaveFormat.Html, options);
// ...