【问题标题】:Azure PDF Sharp not using Unicode fontAzure PDF Sharp 不使用 Unicode 字体
【发布时间】:2015-12-31 00:18:31
【问题描述】:

我们有一个使用 Azure 云服务托管并使用 PDF Sharp 生成 PDF 文档的 C# 网站。

我们使用 Arial Unicode MS Regular 字体,因为我们要显示日文字体。当网站在本地运行时(从 Windows 7 上的 Visual Studio),字体会正确呈现。

我们使用 Azure 的启动脚本将字体安装到云服务服务器上,因为默认情况下它没有安装。我已验证该字体已安装在云服务服务器(Windows Server 2012)上。

在 Azure 托管网站中,日文字体显示为正方形,尽管 PDF 属性确实表明使用的字体是 Arial Unicode MS Regular。

任何想法为什么字体没有在云服务服务器上正确使用?

【问题讨论】:

    标签: c# azure pdf unicode fonts


    【解决方案1】:

    为了解决这个问题,我将问题交叉发布到 PDFSharp 论坛 Post,答案为我指明了创建要使用的 字体解析器 的正确方向:

    1. NuGet 安装 PDFsharp 1.50 beta 2 的 WPF 版本
    2. 使用字体解析器选择字体Using private fonts with PDFsharp 1.50 beta 2 or MigraDoc
    3. 将 unicode 字体添加为项目中的嵌入资源。

    我将此部署到 Azure 云服务并确认正确使用了 unicode 字体。


    PDFSharp 的文档不完整,因为它声明 GDI 构建是用于 .NET 网站的正确构建,但实际上并非如此。相反,使用 FontResolver 的 WPF 构建工作。


    示例

    Global.asax.cs 中设置FontResolver

    PdfSharp.Fonts.GlobalFontSettings.FontResolver = new MyFontResolver();
    

    创建一个名为 MyFontResolver 的新类,它使用嵌入资源中包含的额外字体系列来扩展默认实现。

    字体本身应通过 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;
        }
    }
    

    从嵌入式资源中读取字体数据的助手类。

    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;
            }
        }
    }
    

    在生成PDF的时候像往常一样定义字体,例如:

     var style = document.Styles["Normal"];
     style.Font.Name = "Arial Unicode MS";
     style.Font.Size = 8;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2018-02-27
      • 2015-07-14
      • 2014-10-11
      • 1970-01-01
      相关资源
      最近更新 更多