PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Dec 13, 2017 8:37 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Dec 11, 2015 9:12 pm 
Offline
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 308
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 19, 2016 12:45 am 
Offline

Joined: Tue Jan 19, 2016 12:16 am
Posts: 7
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 19, 2016 7:26 am 
Offline
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 308
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 19, 2016 7:19 pm 
Offline

Joined: Tue Jan 19, 2016 12:16 am
Posts: 7
Good to hear, thanks alot for this example by the way.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group