You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6 WPF票据打印问题:仅出空白页且需直接打印

问题解决与代码修改

核心问题分析

打印空白页的关键原因是错误使用Canvas附加属性定位FixedPage子元素:FixedPage不识别Canvas.Left/TopProperty,导致TextBlock未被放置在可视区域内,最终打印无内容。

修正后的直接打印代码

方式一:使用FixedDocument打印(推荐,适合多页票据)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Printing;

// 构建票据内容
FixedPage fixedPage = new FixedPage();
fixedPage.Width = 320;
fixedPage.Height = 267;

// 构建文本样式
Style textStyle = new Style(typeof(TextBlock));
textStyle.Setters.Add(new Setter(TextBlock.FontWeightProperty, FontWeights.Bold));
textStyle.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Black));

TextBlock textBlock = new TextBlock
{
    Text = content,
    FontSize = fontSize,
    Style = textStyle,
    FontFamily = new FontFamily(fontFamily)
};

// 关键修正:使用FixedPage专属定位方法
FixedPage.SetLeft(textBlock, x);
FixedPage.SetTop(textBlock, y);

fixedPage.Children.Add(textBlock);

// 组装FixedDocument
PageContent pageContent = new PageContent();
((IAddChild)pageContent).AddChild(fixedPage);
FixedDocument fixedDocument = new FixedDocument();
fixedDocument.Pages.Add(pageContent);

// 获取默认打印机并直接打印
var printQueue = LocalPrintServer.GetDefaultPrintQueue();
var printTicket = printQueue.DefaultPrintTicket;
// 设置自定义纸张大小(匹配FixedPage尺寸)
printTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.Custom, fixedPage.Width, fixedPage.Height);

// 创建并执行打印作业
using (var printJob = printQueue.AddJob("Ticket", fixedDocument, false))
{
    // 可选:等待打印完成或处理状态
}

方式二:直接打印FixedPage(适合单页票据)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Printing;

// 构建票据内容
FixedPage fixedPage = new FixedPage();
fixedPage.Width = 320;
fixedPage.Height = 267;

Style textStyle = new Style(typeof(TextBlock));
textStyle.Setters.Add(new Setter(TextBlock.FontWeightProperty, FontWeights.Bold));
textStyle.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Black));

TextBlock textBlock = new TextBlock
{
    Text = content,
    FontSize = fontSize,
    Style = textStyle,
    FontFamily = new FontFamily(fontFamily)
};

FixedPage.SetLeft(textBlock, x);
FixedPage.SetTop(textBlock, y);
fixedPage.Children.Add(textBlock);

// 获取默认打印机
var printQueue = LocalPrintServer.GetDefaultPrintQueue();
var printTicket = printQueue.DefaultPrintTicket;
printTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.Custom, fixedPage.Width, fixedPage.Height);

// 直接打印Visual对象
using (var printJob = printQueue.AddJob())
{
    var writer = PrintQueue.CreateXpsDocumentWriter(printJob);
    writer.Write(fixedPage, printTicket);
}

额外注意事项

  • 确保content、fontSize、fontFamily、x、y变量有有效值,且x/y不超出FixedPage的宽高范围。
  • 如果使用专用票据打印机,可替换PageMediaSizeName.Custom为对应预设尺寸(如PageMediaSizeName.ISOA6),提升兼容性。
  • 程序需具备打印权限,桌面应用默认已拥有,受限制环境需额外配置。

内容的提问来源于stack exchange,提问作者Ari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:42:39