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:
Code:
// 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:
Code:
// We only have Janitor Regular, no Bold, no Italic.
// We allow PDFsharp to simulate Bold and Italic for us.
fontResolver.AddFont("Janitor", XFontStyle.Regular,
@"..\..\..\fonts\janitor\janitor.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:
Code:
// The Ubuntu family has many font faces, so we do not need simulation here.
fontResolver.AddFont("Ubuntu", XFontStyle.Regular,
@"..\..\..\fonts\ubuntufontfamily0.80\ubuntu-R.ttf");
fontResolver.AddFont("Ubuntu", XFontStyle.Italic,
@"..\..\..\fonts\ubuntufontfamily0.80\ubuntu-RI.ttf");
fontResolver.AddFont("Ubuntu", XFontStyle.Bold,
@"..\..\..\fonts\ubuntufontfamily0.80\ubuntu-B.ttf");
fontResolver.AddFont("Ubuntu", XFontStyle.BoldItalic,
@"..\..\..\fonts\ubuntufontfamily0.80\ubuntu-BI.ttf");
When the fonts are registered, you can use them with PDFsharp or MigraDoc like you use any other font.
You can download the complete EZFontResolver class as a ZIP file (about 2041 kiB in size) from my blog:
http://developer.th-soft.com/developer/ ... -migradoc/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.
A sample that shows how to implement IFontResolver in your code can be found here:
viewtopic.php?p=8961#p8961