Windows 11原生系统下PrintDialog打印粗体失效问题求助
解决原生Windows 11下FlowDocument打印粗体失效及引用报错问题
问题核心
FlowDocument中的粗体字体在原生Windows 11打印时不生效,但字号显示正常;升级而来的Win11/Win10无此问题。同时添加WindowsBase、PresentationCore引用时Visual Studio报错,编译失败。
1. 修复原生Win11粗体打印失效问题
原生Win11打印子系统对WPF字体渲染存在兼容性差异,可通过以下方式解决:
- 显式指定带粗体的字体家族
不要仅依赖FontWeight="Bold",直接指定支持粗体的完整字体家族,避免系统字体匹配异常:
若使用嵌入字体,确保包含粗体字重:// 示例:为段落设置明确的粗体字体 var boldPara = new Paragraph(new Run("粗体内容")) { FontFamily = new FontFamily("Segoe UI Bold"), FontSize = 14, FontWeight = FontWeights.Bold };boldPara.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#Segoe UI"); - 强制启用打印兼容渲染模式
打印前调整文档参数,强制使用与打印系统兼容的分页和渲染逻辑:Doc.PageWidth = 816; // 8.5英寸对应96DPI的像素值 Doc.PageHeight = 1056; // 11英寸对应96DPI的像素值 Doc.PagePadding = new Thickness(96); // 1英寸边距 var paginator = ((IDocumentPaginatorSource)Doc).DocumentPaginator; if (paginator is DynamicDocumentPaginator dynamicPaginator) { dynamicPaginator.PageSize = new Size(Doc.PageWidth, Doc.PageHeight); }
2. 解决WindowsBase/PresentationCore引用报错
- 确认项目类型与框架版本
WPF项目默认已包含这些引用,若为非WPF项目(如Console/WinForms),需通过NuGet安装对应包:- 右键项目 → 管理NuGet程序包
- 搜索并安装
WindowsBase和PresentationCore(选择与项目目标框架匹配的版本)
- 清理重建项目
删除项目目录下的bin和obj文件夹,重启Visual Studio后重新编译,可解决缓存导致的引用异常。
3. 打印函数优化(提升稳定性)
优化原函数的打印机验证与错误处理逻辑:
private static bool PrintDocument(FlowDocument Doc, string PrinterName, int Copies, int OrderCode) { try { using (var prnt = new System.Windows.Controls.PrintDialog()) { // 验证目标打印机是否存在 var printServer = new PrintServer(); var targetQueue = printServer.GetPrintQueues() .FirstOrDefault(q => q.Name.Equals(PrinterName, StringComparison.OrdinalIgnoreCase)); if (targetQueue == null) { Utility.LogEvent($"{DateTime.Now}: 打印机 {PrinterName} 不存在"); return false; } prnt.PrintQueue = targetQueue; var paginator = ((IDocumentPaginatorSource)Doc).DocumentPaginator; for (int j = 0; j < Copies; j++) { if (!prnt.PrintDocument(paginator, $"订单{OrderCode} 副本{j+1}")) { Utility.LogEvent($"{DateTime.Now}: 第{j+1}份打印任务被取消"); return false; } } } return true; } catch (Exception ex) { Utility.LogEvent($"{DateTime.Now}; {ex.Message}; {ex.StackTrace}"); return false; } }
内容的提问来源于stack exchange,提问作者Gaetano Riva
相关产品推荐
相关产品推荐

