如何从 C# WPF 中的嵌入字体将字体文件添加到 Stimulsoft 报告

How to Add Font File to Stimulsoft report from embedded font in C# WPF

我使用 C# WPF 和 Stimulsoft

我想在需要显示时发送我的字体文件嵌入到我的报告中的路径

我在 我的 WPF 项目 中嵌入了字体,我这样使用它:

在XAML中:

<Label x:Name="preloader" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Loading . . ." Margin="319,178,48,34" FontFamily="/WpfApp5;component/FNT/#B Titr" FontSize="48" Background="White"/>

字体是从我项目的字体文件夹中嵌入的:

因为我的报告是在 stimulsoft 中生成的,我无法嵌入字体,但我可以将我的字体路径发送给它

通过这个我可以发送我的字体路径 :

为此我尝试了两种方式

C#代码:

1- :

StiFontCollection.AddFontFile(@"pack://application:,,,/FNT/#B Titr");

这个案例会显示这个错误:

System.NotSupportedException: '不支持给定路径的格式。'

2- :

var fntpath = Assembly.GetEntryAssembly().GetManifestResourceStream("WpfApp5.FNT.BTITRBD.TTF");
            StiFontCollection.AddFontFile(fntpath.ToString());

并且在此 fntpath.ToString() 为 null !

如何操作?

请帮忙

AddFontFile 方法需要磁盘上物理文件的路径。您不能传入 pack: URI,因为它不理解该格式。而且您不能只在 Stream 上调用 .ToString(),因为那样不会产生任何有意义的信息。

您需要将字体文件提取到一个临时文件,并将该文件的路径传递给 AddFontFile 方法。

string tempPath = Path.GetTempPath();
string fileName = "WpfApp5.FNT.BTITRBD.TTF";
string fontPath = Path.Combine(tempPath, fileName);
if (!File.Exists(fontPath))
{
    using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream(fileName))
    using (var output = File.Create(fontPath))
    {
        stream.CopyTo(output);
    }
}

StiFontCollection.AddFontFile(fontPath);