TextBlock.FontFamily 打印时不使用字体

TextBlock.FontFamily not using font in print

我正在开发一个简单的 POS WPF 应用程序,用于在没有打印对话框的情况下打印收据。我需要使用窄字体,所以我选择了NK57 Monospace Cd Bk。在 Windows 10 上它可以工作,但在 Windows 7 上我仍然只使用默认字体打印。我尝试了目标打印机(Epson TM-T20II)、普通打印机和打印到 PDF,每次都出现问题。我还尝试了一些不同的 Windows 10 和 Windows 7 台计算机,W10 每次都有效,而 W7 每次都失败。

到目前为止我试过了:

在 Windows 7 上没有任何效果,大部分在 Windows 10 上有效。下面的简化代码示例(使用 PDF 打印以节省纸张)。

var pd = new PrintDialog();
pd.PrintQueue = new LocalPrintServer().GetPrintQueue("Microsoft Print to PDF");
pd.PrintTicket.CopyCount = 2; // number of copies
pd.PrintTicket.PageOrientation = PageOrientation.Portrait;

FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

FixedPage page1 = new FixedPage();
page1.Width = document.DocumentPaginator.PageSize.Width;
page1.Height = document.DocumentPaginator.PageSize.Height;

//var fontReg = new FontFamily("NK57 Monospace Cd Bk");
//var fontReg = new FontFamily(new Uri(AppDomain.CurrentDomain.BaseDirectory + "#NK57 Monospace Cd Bk", UriKind.Absolute), "NK57 Monospace Cd Bk");
//var fontReg = new FontFamily("file:///" + AppDomain.CurrentDomain.BaseDirectory + "#NK57 Monospace Cd Bk");
//var fontReg = new FontFamily("pack://application:,,,./Fonts/#NK57 Monospace Cd Bk");
var fontReg = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#NK57 Monospace Cd Bk");

TextBlock t = new TextBlock();
t.Text = "This is a test";
t.FontFamily = fontReg;
t.FontSize = 14;
page1.Children.Add(t);

PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(page1);
document.Pages.Add(page1Content);

pd.PrintDocument(document.DocumentPaginator, "Print");

有什么问题吗?到目前为止,我唯一可行的解​​决方案是将客户的系统升级到 Windows 10(这迟早会完成,只是会延迟其他已完成的应用程序的发货)。

非常感谢您的帮助。

在 clean virtual Windows 7 中运行后,我回溯了自己,因为我尝试了很多不同的字体样式,我怀疑我最初发布的应用程序使用了错误的字体。这次我确保在客户的 Windows 中安装了正确的字体 7,将代码还原为

var fontReg = new FontFamily("NK57 Monospace Cd Bk");

现在可以使用了。无论如何,我声称所有其他方法(使用 link 到 ttf 文件,将 ttf 文件与应用程序打包)在 Windows 7 上不受支持,因为它在 Windows 10 上工作但没有工作在 Windows 7 上,同样的字体错误没有空间。

谢谢 Peter 和 Heki 的建议。