Azure PDF Sharp not using Unicode font

后端 未结 1 972
攒了一身酷
攒了一身酷 2021-01-13 00:52

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 w

相关标签:
1条回答
  • 2021-01-13 01:51

    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;
    
    0 讨论(0)
提交回复
热议问题