Reputation: 114
Context
In my C# app I am using PDFsharp to create custom PDF documents. I have to use some custom fonts that are not installed on the system beforehand, but instead are available through a resource file as a byte array. The easiest way to achieve that seems to be to use a custom Font Resolver that loads those fonts in the GetFont function. However, once I am implementing my own logic for GetFont, I am no longer able to use any other, standard fonts like Verdana, that are not explicitly loaded in GetFont.
Code
The following code is a simplified version of the code I am using right now.
public class CustomFontResolver : IFontResolver
{
public string DefaultFontName => throw new NotImplementedException();
public byte[] GetFont(string faceName)
{
switch (faceName)
{
case "MyCustomFont":
return Resources.MY_CUSTOM_FONT;
}
// This is the part where I would implement the "standard font loading"-logic
return null;
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if(familyName.Equals("MyCustomFont", StringComparison.CurrentCultureIgnoreCase))
{
return new FontResolverInfo("MyCustomFont");
}
return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);
}
}
This means that, after setting GlobalFontSettings.FontResolver = new CustomFontResolver();
the following line will work with no problems:
var font = new XFont("MyCustomFont", 20, XFontStyle.Regular);
But loading standard fonts like Verdana will fail with an exception, because my GetFont-function returns null for it:
var font = new XFont("Verdana", 20, XFontStyle.Regular);
Question
How can I load standard / already installed fonts as a byte array in my code, or modify the FontResolver in another way to take care of those fonts itself?
I tried using other ways than writing a custom font resolver before, but that did not work out, as PDFsharp does not allow adding fonts to its standard resolver.
I also looked for ways to load fonts and convert them to byte arrays, but could not find any.
Upvotes: 2
Views: 4415
Reputation: 21739
What you want is the default behaviour of the GDI build and the WPF build when using the "original" PDFsharp packages published by PDFsharp-Team. You already found out about that.
PdfSharpCore is a port that also works under Linux and other platforms, but does not automatically include platform-specific fonts.
Upvotes: 1
Reputation: 114
Even though it is embarrassing to admit, the problem seems to be caused by a wrong package. Instead of the actual PdfSharp package I used PdfSharpCore, where PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic)
returned null
. This lead to the thrown exception, and not the implementation of GetFont. Using PdfSharp instead of the Core version everything works well now.
Upvotes: 1
Reputation: 12786
The source of PdfSharp has a if statement that checks if the GlobalFontSettings.FontResolver
is set. If not, it will fall back to its own platform dependent implementation.
What you should do, is in your
// This is the part where I would implement the "standard font loading"-logic
Call the same fall back method:
PlatformFontResolver.ResolveTypeface(string familyName, bool isBold, bool isItalic)
Upvotes: 0