SvgConversionOptions.FontResolver Property

Gets or sets an implementation that will be used for deciding what font to be used for text included in the SVG.

public FontResolver FontResolver { get; set; }
Namespace
PdfToSvg
Package
PdfToSvg.NET (since v0.1.0)

Property Value

FontResolver

Remarks

The default implementation will in first hand try to embed fonts as WOFF files, and if not possible, fallback to detecting standard fonts and assuming the client has those installed. You can implement a custom font resolver for e.g. using custom WOFF or WOFF2 files, or saving the embedded fonts as separate font files. files.

Built-in font resolvers:

ResolverDescription
EmbedOpenTypeExtracts fonts from the PDF, converts them to OpenType format, and then embed them as data URLs in the SVG.
EmbedWoffExtracts fonts from the PDF, converts them to WOFF format, and then embed them as data URLs in the SVG.
LocalFontsTries to match the PDF fonts with commonly available fonts, and references them by name.

Example

The following example will use LocalFonts to convert a PDF to SVG without embedding fonts into the extracted SVG. Instead local fonts assumed to be installed on the client machine are used.

Using local fonts instead of embedding fonts
var conversionOptions = new SvgConversionOptions
{
    FontResolver = FontResolver.LocalFonts,
};

using (var doc = PdfDocument.Open("input.pdf"))
{
    var pageIndex = 0;

    foreach (var page in doc.Pages)
    {
        page.SaveAsSvg($"output-{pageIndex++}.svg", conversionOptions);
    }
}

Custom behavior can be achieved by subclassing FontResolver and implementing the ResolveFont(SourceFont, CancellationToken) method. Here is a custom implementation using a locally installed Open Sans font.

Custom font resolver
class OpenSansFontResolver : FontResolver
{
    public override Font ResolveFont(SourceFont sourceFont, CancellationToken cancellationToken)
    {
        var font = FontResolver.LocalFonts.ResolveFont(sourceFont, cancellationToken);

        if (sourceFont.Name != null &&
            sourceFont.Name.Contains("OpenSans", StringComparison.InvariantCultureIgnoreCase) &&
            font is LocalFont localFont)
        {
            font = new LocalFont("'Open Sans',sans-serif", localFont.FontWeight, localFont.FontStyle);
        }

        return font;
    }
}

Types of substitute fonts that can be returned:

Font typeDescription
LocalFontA font that is assumed to be installed on the machine viewing the SVG.
WebFontUse a provided TrueType, OpenType, WOFF or WOFF2 font. Note that external resources are not allowed in standalone SVG files when displayed in browsers, so if you intend to use external SVG files, you need to return a WebFont instance using data URLs only.

See Also

SvgConversionOptions Class
PdfToSvg Namespace