Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure PDF Sharp未使用Unicode字体_C#_Azure_Pdf_Unicode_Fonts - Fatal编程技术网

C# Azure PDF Sharp未使用Unicode字体

C# Azure PDF Sharp未使用Unicode字体,c#,azure,pdf,unicode,fonts,C#,Azure,Pdf,Unicode,Fonts,我们有一个C#网站,使用Azure云服务托管,并使用PDF Sharp生成PDF文档 我们使用的是Arial Unicode MS常规字体,因为我们可以显示日文字体。当网站在本地运行(从Windows 7上的Visual Studio)时,字体将正确呈现 我们使用Azure的启动脚本将字体安装到云服务服务器上,因为默认情况下没有安装字体。我已验证该字体已安装在云服务服务器(Windows Server 2012)上 在Azure托管的网站中,日文字体显示为正方形,尽管PDF属性确实表明使用的字体

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

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

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

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

你知道为什么云服务服务器上没有正确使用字体吗


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

  • 从安装PDFsharp 1.50 beta 2的WPF版本
  • 使用字体解析器选择字体
  • 将unicode字体作为嵌入资源添加到项目中 我将其部署到Azure云服务,并确认unicode字体使用正确


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


    示例

    Global.asax.cs
    中设置
    FontResolver

    PdfSharp.Fonts.GlobalFontSettings.FontResolver = new MyFontResolver();
    
    创建一个名为
    MyFontResolver
    的新类,该类使用嵌入资源中包含的其他字体系列扩展默认实现

    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;
            }
        }
    }
    
    字体本身应使用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;