如何从文件中加载字体到RDLC报表?

4
在Visual Studio 2005中,我如何从文件中加载字体并在RDLC报告中使用它?
我知道如何从文件中加载字体,感谢这个问题:此处,但我无法在RDLC报告中使用它。
2个回答

1

如果你只需要字体用于一个或两个标题,或者用于条形码列,那么你可以在数据集上创建一个图像类型的列,并将图像预先制作为文本到位图。

// From dreaming in code
/// <summary>
/// Function for converting text to a Bitmap object
/// </summary>
/// <param name="width">Width of the image</param>
/// <param name="height">Height of the image</param>
/// <param name="str">String to be converted</param>
/// <param name="textColor">Color we want the text</param>
/// <param name="recColor">Color we want the background</param>
/// <param name="f">Name of the font we want used</param>
/// <returns></returns>
/// <remarks></remarks>
public Bitmap ConvertTextToBitmap(ref int width, ref int height, ref string str, ref Color textColor, ref Brush recColor, ref string fontName)
{
    using (Bitmap bmp = new Bitmap(width, height)) 
    {
        using (Graphics gfx = Graphics.FromImage((Image)bmp)) 
        {
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            Font font = new Font(fontName, 11, FontStyle.Regular, GraphicsUnit.Pixel);
            gfx.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, bmp.Width, bmp.Height));
            gfx.FillRectangle(recColor, 0, 0, width, height);
            gfx.DrawString(str, font, new SolidBrush(textColor), 2, 3);
            bmp.Save(Application.StartupPath + "\\" + str + ".bmp", ImageFormat.Bmp);
            return bmp;
        }
    }
}

你也可以创建一个自定义报表项来替换默认的文本框等,但从所有的经验来看,这些都是非常麻烦的。


我可以在报表上动态完成这个吗? - FerranB
我无法在磁盘上写入,那就不好了... :-( - FerranB
使用此示例,您必须在启动RDLC之前预先制作数据集中的图像。您不需要写入磁盘,这只是糟糕的示例代码,抱歉。如果您想在报告内动态执行此操作,则需要创建自定义报告项或自定义报告渲染器,两者都很困难。 - TFD

1

看起来唯一的方法是在Windows上安装字体,然后像系统中的任何其他可用字体一样使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接