FontResolver Class
Resolves which font to be used for text in the SVG, for a given PDF font name.
public abstract class FontResolver
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 type | Description |
---|---|
LocalFont | A font that is assumed to be installed on the machine viewing the SVG. |
WebFont | Use 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. |
InlinedFont | Inlines the glyphs from the font as paths and other elements within the SVG. This differs from WebFont, which are embedded as font files in the SVG. The inlined glyphs cannot be selected as text in the SVG. Currently only Type 3 fonts can be inlined, but more font types might be supported in the future. |
Constructors
FontResolver | Initializes a new instance of the FontResolver class |
Properties
Default | Gets the default font resolver used when no resolver is explicitly specified. Currently EmbedWoff is the default font resolver, but this can change in the future. | |
EmbedOpenType | Font resolver converting fonts in the PDF to OpenType (.otf) format and embedding them in the output SVG. If the font cannot be converted, the resolver in first hand tries to inline the glyphs. If this is not possible, the resolver falls back to the LocalFonts resolver. | |
EmbedWoff | Font resolver converting fonts in the PDF to WOFF format and embedding them in the output SVG. If the font cannot be converted, the resolver in first hand tries to inline the glyphs. If this is not possible, the resolver falls back to the LocalFonts resolver. | |
LocalFonts | Font resolver substituting fonts in the PDF with commonly available fonts. No fonts are embedded or inlined in the resulting SVG. The resolved fonts need to be available on the viewing machine. |
Methods
ResolveFont | Resolves which font to be used for text in the SVG, for a given source PDF font. | |
ResolveFontAsync | Resolves asynchronously which font to be used for text in the SVG, for a given source PDF font. |