PDFSharp private fonts for azure 1.50

后端 未结 2 1101
天涯浪人
天涯浪人 2021-01-20 22:07

I have downloaded and installed PDFSharp 1.5 and I am having trouble using private fonts. I have created in testing a pdf creator and it works great. When I load it into A

2条回答
  •  别那么骄傲
    2021-01-20 22:31

    This is for PdfSharp 1.5 beta3b. Here is a complete and fixed example based on links from other answers, and other questions - but for Arial.

    Add the fonts you want to your project - in my example below I put Arial in MyProject\fonts\arial\arial.ttf etc. Set each font file as an embedded resource (properties -> build action).

    Apply the font resolver using the static call like this:

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

    Here's the font resolver class:

    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;
        }
    
        /// 
        /// Returns the specified font from an embedded resource.
        /// 
        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;
    
        /// 
        /// Ensure the font resolver is only applied once (or an exception is thrown)
        /// 
        internal static void Apply()
        {
            if (OurGlobalFontResolver == null || GlobalFontSettings.FontResolver == null)
            {
                if (OurGlobalFontResolver == null)
                    OurGlobalFontResolver = new MyFontResolver();
    
                GlobalFontSettings.FontResolver = OurGlobalFontResolver;
            }
        }
    }
    

提交回复
热议问题