PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

EZFontResolver: a generic font resolver for PDFsharp and Mig
http://forum.pdfsharp.com/viewtopic.php?f=8&t=3244
Page 1 of 1

Author:  TH-Soft [ Fri Dec 11, 2015 9:12 pm ]
Post subject:  EZFontResolver: a generic font resolver for PDFsharp and Mig

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

Author:  seeker25 [ Tue Jan 19, 2016 12:45 am ]
Post subject:  Re: EZFontResolver: a generic font resolver for PDFsharp and

Just wanted to let you know, if there are any errors inside of your PDFSharp / MigraDocs code.. It will try to resolve using the font Courier New. If your resolver doesn't have Courier New, it will cause an error complaining about the font.

Author:  TH-Soft [ Tue Jan 19, 2016 7:26 am ]
Post subject:  Re: EZFontResolver: a generic font resolver for PDFsharp and

seeker25 wrote:
Just wanted to let you know, if there are any errors inside of your PDFSharp / MigraDocs code.. It will try to resolve using the font Courier New. If your resolver doesn't have Courier New, it will cause an error complaining about the font.

If the supplied FontResolver cannot find the font, then the default font resolver of PDFsharp will be used. And the default font resolver is platform specific and will behave differently for GDI, WPF, Core, Silverlight.

If there are errors that I can replicate then I might try to improve the font resolver.

Maybe I will change my EZFontResolver to support a Catch All Default font. This would allow you to specify a substitute for Courier New.

Author:  seeker25 [ Tue Jan 19, 2016 7:19 pm ]
Post subject:  Re: EZFontResolver: a generic font resolver for PDFsharp and

Good to hear, thanks alot for this example by the way.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/