1
votes

I am using a webapp on azure to create a PDF using PDFsharp 1.32. As it is a Azure webapp I cannot install fonts.

Therefore I am using the following with embedded font options:

var globalFontCollection = XPrivateFontCollection.Global;
_fontBytes = File.ReadAllBytes(_pathToFont);   
globalFontCollection.AddFont(_fontBytes, _fontName);

However this does not work. There is no error just acts as if the font is not known and falls back to the default. If I install the font it works so I know the rest of the code is correct.

Other code shows using AddFont with different parameters however those are not available to me and looking at the source code this is the only implemented method.

Does anyone know the correct code to allow us to use non installed fonts?

1

1 Answers

1
votes

The font handling of PDFsharp 1.50 was overhauled - also with Azure and Medium Trust in mind. I'd recommend using PDFsharp 1.50 (latest version is beta 3) or later for Azure. I think the WPF build will be appropriate.
The new IFontResolver gives you better control of font handling.
The samples package for PDFsharp 1.50 includes a sample. Alternatively you can use EZFontResolver from a third party.

With respect to PDFsharp 1.32: there are different builds that expose different overloads for AddFont. I'd also recommend the WPF build for Azure.
Font handling is platform-specific (GDI+ or WPF). It can be difficult to get the font names "correct" to have the platform use the custom font.
PDFsharp 1.32 includes a working WPF sample that uses a font from a resource. You can use this sample to get started.

GDI+ is not an option for Azure web sites as far as I know. So WPF is the way to go. I would use version 1.50 with the new IFontResolver interface. With the EZFontResolver implementation it is easy to use. There is a sample for PDFsharp 1.50 that shows an implementation of IFontResolver that can be used as a template if you want to do it without EZFontResolver.