PDFsharp on Azure with Custom Fonts

时光毁灭记忆、已成空白 提交于 2020-01-14 03:08:13

问题


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:


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.



来源:https://stackoverflow.com/questions/36177186/pdfsharp-on-azure-with-custom-fonts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!