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.

Built-in font resolvers

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

Text representation

PDF documents store text encoded as character codes, and provide mappings from character codes to font glyphs and Unicode characters. Some documents map multiple character codes to the same Unicode character, giving PdfToSvg.NET a choice. Exporting both character codes as the same Unicode character results in good text representation but potentially visually inaccurate SVG’s. Remapping one of the character codes to another Unicode character ensures visually accurate SVG’s at the cost of inaccurate text representation if text is exported from the SVG.

When exporting text using FontResolver.LocalFonts, the library will use the Unicode mapping specified by the document, providing more accurate text representation.

When exporting text using FontResolver.EmbedOpenType or FontResolver.EmbedWoff, the library will remap duplicate character codes to characters in the Private Use Areas, making sure the exported SVG’s are visually accurate, but text might appear as a series of question marks, ������, in the SVG markup. If you intend to extract text from the SVG, consider exporting the SVG using local fonts instead.

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 pageNo = 1;

    foreach (var page in doc.Pages)
    {
        page.SaveAsSvg($"output-{pageNo++}.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;
    }
}

See Also

SvgConversionOptions Class
PdfToSvg Namespace