PDF用于azure 1.50的私有字体

时间:2014-12-22 16:47:19

标签: fonts private pdfsharp

我已下载并安装了PDFSharp 1.5,但我在使用私有字体时遇到问题。我在测试pdf创建者时创建了它,效果很好。当我将它加载到Azure时,它给我的错误是无法加载字体。做了研究,发现他们没有任何加载的字体,所以我必须使用私人字体。我只能找到旧1.3版本的示例,并且方法已更改为新版本。有人可以使用新版本的PDFSharp向我展示一个简单的例子吗?

感谢约翰

1 个答案:

答案 0 :(得分:4)

这适用于PdfSharp 1.5 beta3b。以下是基于其他答案和其他问题的链接的完整且固定的示例 - 但对于Arial。

将您想要的字体添加到项目中 - 在我的示例中,我将Arial放入MyProject\fonts\arial\arial.ttf等。将每个字体文件设置为嵌入资源(属性 - >构建操作)。

使用静态调用应用字体解析器,如下所示:

MyFontResolver.Apply(); // Ensures it's only applied once

这是字体解析器类:

class MyFontResolver : IFontResolver
{
    public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
    {
        // Ignore case of font names.
        var name = familyName.ToLower().TrimEnd('#');

        // Deal with the fonts we know.
        switch (name)
        {
            case "arial":
                if (isBold)
                {
                    if (isItalic)
                        return new FontResolverInfo("Arial#bi");
                    return new FontResolverInfo("Arial#b");
                }
                if (isItalic)
                    return new FontResolverInfo("Arial#i");
                return new FontResolverInfo("Arial#");
        }

        // We pass all other font requests to the default handler.
        // When running on a web server without sufficient permission, you can return a default font at this stage.
        return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);
    }

    public byte[] GetFont(string faceName)
    {
        switch (faceName)
        {
            case "Arial#":
                return LoadFontData("MyProject.fonts.arial.arial.ttf");;

            case "Arial#b":
                return LoadFontData("MyProject.fonts.arial.arialbd.ttf");;

            case "Arial#i":
                return LoadFontData("MyProject.fonts.arial.ariali.ttf");

            case "Arial#bi":
                return LoadFontData("MyProject.fonts.arial.arialbi.ttf");
        }

        return null;
    }

    /// <summary>
    /// Returns the specified font from an embedded resource.
    /// </summary>
    private byte[] LoadFontData(string name)
    {
        var assembly = Assembly.GetExecutingAssembly();

        // Test code to find the names of embedded fonts - put a watch on "ourResources"
        //var ourResources = assembly.GetManifestResourceNames();

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

    internal static MyFontResolver OurGlobalFontResolver = null;

    /// <summary>
    /// Ensure the font resolver is only applied once (or an exception is thrown)
    /// </summary>
    internal static void Apply()
    {
        if (OurGlobalFontResolver == null || GlobalFontSettings.FontResolver == null)
        {
            if (OurGlobalFontResolver == null)
                OurGlobalFontResolver = new MyFontResolver();

            GlobalFontSettings.FontResolver = OurGlobalFontResolver;
        }
    }
}