EZFontResolver implements the IFontResolver interface that PDFsharp uses.
In your program, you simply call the AddFont method of EZFontResolver. You simply pass the filename of your font or a byte array containing the font data.
EZFontResolver just needs a little help: you have to specify a family name and you also have to indicate the style of the font you pass (regular, bold, italic, bold italic) and whether you want to enable simulation of bold and italic in PDFsharp.
Let’s look at some code. This is the code that creates an instance of EZFontResolver and assigns it to PDFsharp:
// Get the EZFontResolver. EZFontResolver fontResolver = EZFontResolver.Get; // Assign it to PDFsharp. GlobalFontSettings.FontResolver = fontResolver;
One sample font I tried is Janitor. There is only a single file with Janitor Regular, therefore I enable simulation of bold and italic in PDFsharp:
// We only have Janitor Regular, no Bold, no Italic. // We allow PDFsharp to simulate Bold and Italic for us. fontResolver.AddFont("Janitor", XFontStyle.Regular, @"......fontsjanitorjanitor.ttf", true, true);
I also tested with four font faces from the Ubuntu family. Since I have faces for bold and italic, simulation is not needed here:
// The Ubuntu family has many font faces, so we do not need simulation here. fontResolver.AddFont("Ubuntu", XFontStyle.Regular, @"......fontsubuntufontfamily0.80ubuntu-R.ttf"); fontResolver.AddFont("Ubuntu", XFontStyle.Italic, @"......fontsubuntufontfamily0.80ubuntu-RI.ttf"); fontResolver.AddFont("Ubuntu", XFontStyle.Bold, @"......fontsubuntufontfamily0.80ubuntu-B.ttf"); fontResolver.AddFont("Ubuntu", XFontStyle.BoldItalic, @"......fontsubuntufontfamily0.80ubuntu-BI.ttf");
When the fonts are registered, you can use them with PDFsharp or MigraDoc like you use any other font.
A final note: EZFontResolver is implemented as a singleton. Only one object of class will be created and each call of Get will return the same object. You can add the fonts only once and you will get an exception if a font with the same name already exists. If your program creates several documents, then register all fonts that are needed before creating the first PDF file.
You can download the complete EZFontResolver class as a ZIP file (about 2041 kiB in size).
The ZIP file contains a solution with a sample program and requires the PDFsharp package from NuGet. To use EZFontResolver with your project, just copy the file EZFontResolver.cs into your folder and add it to your project.
EZFontResolver is meant to make using private fonts more easy. Complete source code is in the ZIP file.
There is another example that implements IFontResolver: Sample that implements IFontResolver.