Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

c# - Azure PDF Sharp not using Unicode font

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?

The PDF generated by Windows 2012 server running on Azure. The Japanese fonts rendered as sqaures but the properties indicate the unicode font was used.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Install the WPF build of PDFsharp 1.50 beta 2 from NuGet
  2. Use a Font Resolver to select the font Using private fonts with PDFsharp 1.50 beta 2 or MigraDoc
  3. 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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...