To solve this problem I cross-posted the question to the PDFSharp Forum Post and an answer pointed me in the right direction in creating a Font Resolver to be used:
- Install the WPF build of PDFsharp 1.50 beta 2 from NuGet
- Use a Font Resolver to select the font Using private fonts with PDFsharp 1.50 beta 2 or MigraDoc
- Add the unicode font as an embedded resource in the project.
I deployed this to Azure Cloud Services and confirmed that the unicode font was correctly used.
The documentation for PDFSharp is incomplete since it states that the GDI build is the correct one to use for .NET websites when in fact it is not in this case. Instead the WPF build using a FontResolver worked.
Example
Setup the FontResolver
in Global.asax.cs
:
PdfSharp.Fonts.GlobalFontSettings.FontResolver = new MyFontResolver();
Create a new class called MyFontResolver
that extends the default implementation with handly additional font families which are included in embedded resources.
The font(s) themselves should be added to the font directory with build action = Embedded Resource
.
public class MyFontResolver : IFontResolver
{
public FontResolverInfo ResolveTypeface(string familyName,
bool isBold,
bool isItalic)
{
// Ignore case of font names.
var name = familyName.ToLower();
// Add fonts here
switch (name)
{
case "arial unicode ms":
return new FontResolverInfo("ArialUnicodeMS#");
}
//Return a default font if the font couldn't be found
//this is not a unicode font
return PlatformFontResolver.ResolveTypeface("Arial", isBold, isItalic);
}
// Return the font data for the fonts.
public byte[] GetFont(string faceName)
{
switch (faceName)
{
case "ArialUnicodeMS#": return FontHelper.ArialUnicodeMS; break;
}
return null;
}
}
Helper class that reads font data from embedded resources.
public static class FontHelper
{
public static byte[] ArialUnicodeMS
{
//the font is in the folder "/fonts" in the project
get { return LoadFontData("MyApp.fonts.ARIALUNI.TTF"); }
}
/// Returns the specified font from an embedded resource.
static byte[] LoadFontData(string name)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(name))
{
if (stream == null)
throw new ArgumentException("No resource with name " + name);
int count = (int)stream.Length;
byte[] data = new byte[count];
stream.Read(data, 0, count);
return data;
}
}
}
Define the font as usual when generating the PDF, for example:
var style = document.Styles["Normal"];
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 8;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…