FontSettings.SetFontsSources

FontSettings SetFontsSources method. Sets the sources where Wordize looks for TrueType fonts when rendering documents or embedding fonts.

SetFontsSources(FontSourceBase[])

Sets the sources where Wordize looks for TrueType fonts when rendering documents or embedding fonts.

public void SetFontsSources(FontSourceBase[] sources)
ParameterTypeDescription
sourcesFontSourceBase[]An array of sources that contain TrueType fonts.

Remarks

By default, Wordize looks for fonts installed to the system.

Setting this property resets the cache of all previously loaded fonts.

Examples

Implementation of IWarningCallback to display warnings generated when reading font sources.

private class FontReadingWarningCallback : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        if (info.Source == WarningSource.Font)
        {
            Console.WriteLine(info.Description);
        }
    }
}

Demonstrates how to specify multiple font sources and generate warnings when reading fonts from these sources.

IWarningCallback fontReadingWarningCallback = new FontReadingWarningCallback();

FolderFontSource firstSource = new FolderFontSource(MyDir + "MyFonts", true);
firstSource.WarningCallback = fontReadingWarningCallback;
FolderFontSource secondSource = new FolderFontSource(MyDir + "MyFonts2", true);
secondSource.WarningCallback = fontReadingWarningCallback;

// Specify font setting for concrete document conversion operation.
ConverterContext context = new ConverterContext();
context.FontSettings = new FontSettings();
context.FontSettings.SetFontsSources(new FontSourceBase[] { firstSource, secondSource });

Converter.Create(context)
    .From(MyDir + "Simple.docx")
    .To(ArtifactsDir + "FontSubstitution.pdf")
    .Execute();

See Also


SetFontsSources(FontSourceBase[], Stream)

Sets the sources where Wordize looks for TrueType fonts and additionally loads previously saved font search cache.

public void SetFontsSources(FontSourceBase[] sources, Stream cacheInputStream)
ParameterTypeDescription
sourcesFontSourceBase[]An array of sources that contain TrueType fonts.
cacheInputStreamStreamInput stream with saved font search cache.

Remarks

Loading previously saved font search cache will speed up the font cache initialization process. It is especially useful when access to font sources is complicated (e.g. when fonts are loaded via network).

When saving and loading font search cache, fonts in the provided sources are identified via cache key. For the fonts in the SystemFontSource and FolderFontSource cache key is the path to the font file. For MemoryFontSource and StreamFontSource cache key is defined in the CacheKey and CacheKey properties respectively. For the FileFontSource cache key is either CacheKey property or a file path if the CacheKey is null.

It is highly recommended to provide the same font sources when loading cache as at the time the cache was saved. Any changes in the font sources (e.g. adding new fonts, moving font files or changing the cache key) may lead to the inaccurate font resolving by Wordize.

Examples

Shows how to speed up the font cache initialization process.

const string cacheKey1 = "NoticiaText-Regular";
const string cacheKey2 = "NoticiaText-Bold";
FontSettings parsedFonts = new FontSettings();
FontSettings loadedCache = new FontSettings();

// Set font sources.
parsedFonts.SetFontsSources(new FontSourceBase[]
{
        new FileFontSource(FontsDir + "NoticiaText-Regular.ttf", 0, cacheKey1),
        new FileFontSource(FontsDir + "NoticiaText-Bold", 0, cacheKey2)
});

using (MemoryStream cacheStream = new MemoryStream())
{
    parsedFonts.SaveSearchCache(cacheStream);
    Console.WriteLine(cacheStream.Position);

    // Load the font into another font setting instance using cached information.
    loadedCache.SetFontsSources(new FontSourceBase[]
    {
        new FileFontSource(FontsDir + "NoticiaText-Regular.ttf", 0, cacheKey1),
        new FileFontSource(FontsDir + "NoticiaText-Bold", 0, cacheKey2)
    }, cacheStream);
}

See Also