简体   繁体   English

我们如何在Azure应用服务的RDLC中使用第三方字体?

[英]How can we use a third party font in RDLC on Azure app service?

We have an application that uses a bar code font within an RDLC report, and we want to migrate this from a VM to Azure app services (formerly web apps). 我们有一个在RDLC报表中使用条形码字体的应用程序,并且我们希望将其从VM迁移到Azure应用程序服务(以前是Web应用程序)。 The issue here is that we obviously don't have the ability to install the font. 这里的问题是,我们显然没有安装字体的能力。 I am pretty sure RDLC uses GDI+ to render the reports, so I was hoping I could find a method/property on the LocalReport that would give me the ability to set some sort of PrivateFontCollection, but I have hit a brick wall. 我非常确定RDLC使用GDI +来呈现报告,所以我希望可以在LocalReport上找到一种方法/属性,该方法/属性可以让我设置某种PrivateFontCollection,但是我遇到了麻烦。

So I guess I have a couple questions that might help get this figured out, otherwise we will need to come up with another reporting solution: 因此,我想我有几个问题可能有助于弄清楚这个问题,否则我们将需要提出另一个报告解决方案:

  1. Is there a generic way to temporarily install a font in .NET that can be used by the current application? 有没有一种通用方法可以在.NET中临时安装当前应用程序可以使用的字体?
  2. Is there a way to specify a PrivateFontCollection anywhere in a LocalReport object? 有没有一种方法可以在LocalReport对象中的任何地方指定PrivateFontCollection?

RDLC report refer to the font by the family name. RDLC报告通过家族名称引用字体。 To use a third party font, we have to actually install the font on your production machine. 要使用第三方字体,我们必须在生产机器上实际安装该字体。

Here is feedback from SQL Server Reporting Services team. 这是SQL Server Reporting Services团队的反馈。

The ReportViewer's rendering extensions do not support PrivateFontCollection. ReportViewer的呈现扩展不支持PrivateFontCollection。

As a workaround, you could generate the barcode into a image without using private font and put the image on your RDLC report. 作为一种解决方法,您可以在不使用私有字体的情况下将条形码生成为图像,然后将图像放入RDLC报告中。 Here is a source code and test code to generate a barcode image file. 这是生成条形码图像文件的源代码和测试代码。

class Program
{
    static void Main(string[] args)
    {
        BarCodeGenerator barCode = new BarCodeGenerator("123456789012345");
        barCode.SaveImage("F:\\abc.jpeg");
    }
}

public class BarCodeGenerator
{
    public BarCodeGenerator(string code, int barHeight = 200, int imageWidth = 420, int imageHeigth = 240)
    {
        _barCode = code;
    }

    public void SaveImage(string filePath)
    {
        Bitmap bmp = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        Graphics g = Graphics.FromImage(bmp);

        g.FillRectangle(Brushes.White, 0, 0, Width, Height);

        String intercharacterGap = "0";
        String str = '*' + _barCode.ToUpper() + '*';
        int strLength = str.Length;

        for (int i = 0; i < _barCode.Length; i++)
        {
            if (alphabet39.IndexOf(_barCode[i]) == -1 || _barCode[i] == '*')
            {
                g.DrawString("INVALID BAR CODE TEXT", new Font("Arial", 12), Brushes.Red, 10, 10);
                return;
            }
        }

        String encodedString = "";

        for (int i = 0; i < strLength; i++)
        {
            if (i > 0)
                encodedString += intercharacterGap;

            encodedString += coded39Char[alphabet39.IndexOf(str[i])];
        }

        int encodedStringLength = encodedString.Length;
        int widthOfBarCodeString = 0;
        double wideToNarrowRatio = 3;


        if (align != AlignType.Left)
        {
            for (int i = 0; i < encodedStringLength; i++)
            {
                if (encodedString[i] == '1')
                    widthOfBarCodeString += (int)(wideToNarrowRatio * (int)weight);
                else
                    widthOfBarCodeString += (int)weight;
            }
        }

        int x = 0;
        int wid = 0;
        int yTop = 0;
        SizeF hSize = g.MeasureString(headerText, headerFont);
        SizeF fSize = g.MeasureString(_barCode, footerFont);

        int headerX = 0;
        int footerX = 0;

        if (align == AlignType.Left)
        {
            x = leftMargin;
            headerX = leftMargin;
            footerX = leftMargin;
        }
        else if (align == AlignType.Center)
        {
            x = (Width - widthOfBarCodeString) / 2;
            headerX = (Width - (int)hSize.Width) / 2;
            footerX = (Width - (int)fSize.Width) / 2;
        }
        else
        {
            x = Width - widthOfBarCodeString - leftMargin;
            headerX = Width - (int)hSize.Width - leftMargin;
            footerX = Width - (int)fSize.Width - leftMargin;
        }

        if (showHeader)
        {
            yTop = (int)hSize.Height + topMargin;
            g.DrawString(headerText, headerFont, Brushes.Black, headerX, topMargin);
        }
        else
        {
            yTop = topMargin;
        }

        for (int i = 0; i < encodedStringLength; i++)
        {
            if (encodedString[i] == '1')
                wid = (int)(wideToNarrowRatio * (int)weight);
            else
                wid = (int)weight;

            g.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, x, yTop, wid, height);

            x += wid;
        }

        yTop += height;

        if (showFooter)
            g.DrawString(_barCode, footerFont, Brushes.Black, footerX, yTop);

        g.Flush();
        using (FileStream stream = File.Create(filePath))
        {
            bmp.Save(stream, ImageFormat.Jpeg);
        }  
    }

    private string alphabet39 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";

    private string[] coded39Char =
    {
        /* 0 */ "000110100", 
        /* 1 */ "100100001", 
        /* 2 */ "001100001", 
        /* 3 */ "101100000",
        /* 4 */ "000110001", 
        /* 5 */ "100110000", 
        /* 6 */ "001110000", 
        /* 7 */ "000100101",
        /* 8 */ "100100100", 
        /* 9 */ "001100100", 
        /* A */ "100001001", 
        /* B */ "001001001",
        /* C */ "101001000", 
        /* D */ "000011001", 
        /* E */ "100011000", 
        /* F */ "001011000",
        /* G */ "000001101", 
        /* H */ "100001100", 
        /* I */ "001001100", 
        /* J */ "000011100",
        /* K */ "100000011", 
        /* L */ "001000011", 
        /* M */ "101000010", 
        /* N */ "000010011",
        /* O */ "100010010", 
        /* P */ "001010010", 
        /* Q */ "000000111", 
        /* R */ "100000110",
        /* S */ "001000110", 
        /* T */ "000010110", 
        /* U */ "110000001", 
        /* V */ "011000001",
        /* W */ "111000000", 
        /* X */ "010010001", 
        /* Y */ "110010000", 
        /* Z */ "011010000",
        /* - */ "010000101", 
        /* . */ "110000100", 
        /*' '*/ "011000100",
        /* $ */ "010101000",
        /* / */ "010100010", 
        /* + */ "010001010", 
        /* % */ "000101010", 
        /* * */ "010010100"
    };

    public enum AlignType
    {
        Left, Center, Right
    }

    public enum BarCodeWeight
    {
        Small = 1, Medium, Large
    }

    private AlignType align = AlignType.Center;
    private string _barCode = "1234567890";
    private int leftMargin = 10;
    private int topMargin = 20;
    private int height = 200;
    private bool showHeader = false;
    private bool showFooter = false;
    private String headerText = "BarCode Demo";
    private BarCodeWeight weight = BarCodeWeight.Small;
    private Font headerFont = new Font("Courier", 18);
    private Font footerFont = new Font("Courier", 8);

    private int _height = 240;
    public int Height
    {
        get { return _height; }
        set { _height = value; }
    }

    private int _width = 420;
    public int Width
    {
        get { return _width; }
        set { _width = value; }
    }
}

Here is the barcode image generated by upper code. 这是由上位代码生成的条形码图像。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Azure App Service中安装自定义字体 - how to install custom font in azure app service 如何在第三方注册中找到客户端IP? - How can I find client IP on Third-party registration? 在Windows Azure上托管第三方应用程序 - Host a third party application on windows azure 如何使用 AzCopy 使用 cmd 在 Azure Web 应用服务中工作 - How to use AzCopy to work in Azure web app service using cmd 如何使用 Service Principal/Managed Identity 访问 Azure App Configuration? - How to use Service Principal/Managed Identity to access Azure App Configuration? 我可以使用第三方IdP(不是Windows Azure AD)来实现VS 2013中引入的SSO身份验证吗? - Can I implement SSO authentication that introduced in VS 2013 with a third party IdP (not Windows Azure AD)? 如何查看 Azure 应用服务日志文件? - How can I view the Azure App Service log files? 如何在VPC上部署Azure应用服务 - How can I deploy an Azure App Service on VPC 如何将ASP.net Identity与第三方REST服务一起使用进行身份验证? - How can I use ASP.net Identity with a 3rd Party REST service for authentication? 如何在没有第三方服务和没有Windows服务的情况下实现ASP.NET中无限运行的后台任务? - How to implement infinitely running background tasks in ASP.NET without third party services and without Windows Service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM