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

问题描述 投票:0回答:2

我使用 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 !

如何做到这一点?

请帮忙

c# wpf fonts embed stimulsoft
2个回答
1
投票

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

您需要将字体文件提取到临时文件,并将该文件的路径传递给

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

0
投票
var report = new StiReport();
var reportPath = System.Web.Hosting.HostingEnvironment.MapPath($@"~/Reports/{reportItem.FileName}");
Stimulsoft.Base.StiFontCollection.AddFontFile(System.Web.Hosting.HostingEnvironment.MapPath($@"~/fonts/Samim.ttf"));
Stimulsoft.Base.StiFontCollection.AddFontFile(System.Web.Hosting.HostingEnvironment.MapPath($@"~/fonts/IRANSansWeb.ttf"));
report.Load(reportPath);
report.RegBusinessObject("customer", customerModel);
report.RegBusinessObject("receipt", receiptModel);
report.RegBusinessObject("order", orderModel);
report.Render(false);
© www.soinside.com 2019 - 2024. All rights reserved.