We have a C# website that is hosted using Azure Cloud Services and uses PDF Sharp to generate PDF documents.
We are using the Arial Unicode MS Regular font since we have Japanese fonts to display. When the website is run locally (from Visual Studio on Windows 7) the fonts render correctly.
We use a start-up script for Azure to install the font onto the Cloud Service servers since its not installed by default. I have verified that the font is installed on the Cloud Service servers (Windows Server 2012).
In the Azure hosted website the Japanese fonts appear as squares, even though the PDF properties do indicate that the font used is Arial Unicode MS Regular.
Any ideas why the font is not being correctly used on the Cloud Service servers?
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:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With