WPF 获取电脑打印机列表并使用 58mm 热敏打印机打印
参考
- 豆包
- DeepSeek
- https://coreylyn.github.io/Prism-Documentation-zh/#/commands/commanding
- https://www.cnblogs.com/1312mn/p/18633900
- https://www.cnblogs.com/chonglu/p/15150369.html
- https://www.cnblogs.com/Health/archive/2012/02/17/2355924.html
- https://blog.csdn.net/qq_44618510/article/details/130546769
- https://blog.csdn.net/zcaixzy5211314/article/details/80784329
- https://www.cnblogs.com/dongweian/p/14361943.html
- https://learn.microsoft.com/en-us/dotnet/api/system.printing.printqueue.userprintticket?view=windowsdesktop-9.0#system-printing-printqueue-userprintticket
- https://www.cnblogs.com/DoNetCoder/p/4385257.html
- https://developer.aliyun.com/article/1315761
- https://www.cnblogs.com/tianma3798/p/12814498.html
- https://www.cnblogs.com/AtTheMoment/p/16175898.html
- https://v4.cecdn.yun300.cn/100001_2103235147/芯烨58系列中文编程手册.pdf
- https://learn.microsoft.com/zh-cn/dotnet/api/system.printing.localprintserver?view=windowsdesktop-9.0&viewFallbackFrom=net-6.0
- https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.documents.flowdocument?view=windowsdesktop-8.0
- https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.documents.flowdocument.pagepadding?view=windowsdesktop-8.0#system-windows-documents-flowdocument-pagepadding
环境
| 软件/系统 | 版本 | 说明 |
|---|---|---|
| Windows | windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器 | |
| Microsoft Visual Studio | Community 2022 (64 位) - Current 版本 17.14.5 | |
| .NET | 6.0 | |
| Prism Template Pack | 2.4.1 | 本项目基于该扩展创建 |
| Prism.DryIoc | 8.1.97 | 项目依赖 |
| MaterialDesignThemes | 5.2.1 | 项目依赖 |
| 芯烨 58/80/76系列通用驱动 | 2025.04.14 | [下载页面] [下载地址] |
| 硬件/设备 | 版本 | 说明 |
|---|---|---|
| 热敏打印机 | 芯烨 XP-58IIH | |
| 热敏打印纸 | 57x50 无芯管 |
正文
运行截图
-
小票截图


-
软件界面截图

核心代码
- MainWindowViewModel.cs
using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.ObjectModel; using System.Diagnostics; using System.Printing; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Markup; using System.Windows.Media; namespace WPFPrint58mmTest.ViewModels { public class MainWindowViewModel : BindableBase { private string _title = "测试打印APP"; public string Title { get { return _title; } set { SetProperty(ref _title, value); } } // 打印内容 private string _textContent = ""; public string TextContent { get { return _textContent; } set { SetProperty(ref _textContent, value); } } // 选中的打印机 private PrinterInfo _printer = new(); public PrinterInfo Printer { get { return _printer; } set { SetProperty(ref _printer, value); } } // 打印机列表 private ObservableCollection<PrinterInfo> _printers = new(); public ObservableCollection<PrinterInfo> Printers { get { return _printers; } set { SetProperty(ref _printers, value); } } public MainWindowViewModel() { // 获取打印机列表 GetPrinters(); // 绑定命令 SubmitCommand = new DelegateCommand<object>(Submit); } public void GetPrinters() { Printers.Clear(); using (LocalPrintServer printServer = new LocalPrintServer()) { // 默认打印机 PrintQueue pq = printServer.DefaultPrintQueue; foreach (PrintQueue queue in printServer.GetPrintQueues()) { if (queue.QueuePrintProcessor != null) { Printers.Add(new PrinterInfo() { Name = queue.Name, localPrintServer = printServer, IsDefault = pq.Name == queue.Name }); } } } } public DelegateCommand<object> SubmitCommand { get; private set; } void Submit(object parameter) { PrintDialog printDialog = new PrintDialog(); // 设置打印份数 printDialog.PrintTicket.CopyCount = 1; // 创建打印内容 FlowDocument doc = CreateReceiptDocument(); doc.PageHeight = printDialog.PrintableAreaHeight; doc.PageWidth = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket).PageImageableArea.ExtentWidth; Debug.WriteLine(doc.PageWidth); // 打印文档 printDialog.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "小票打印"); MessageBox.Show("打印任务已发送"); } private FlowDocument CreateReceiptDocument() { // 创建适用于58mm小票的打印文档 FlowDocument doc = new FlowDocument { // 页面边距 PagePadding = new Thickness(5, 5, 5, 20), ColumnGap = 0, ColumnWidth = double.MaxValue, FontFamily = new FontFamily("微软雅黑"), // 宋体 FontSize = 18, }; // 添加标题 Paragraph title = new Paragraph(new Run("=== 销售小票 ===")) { FontSize = 16, FontWeight = FontWeights.Bold, TextAlignment = TextAlignment.Center }; doc.Blocks.Add(title); // 添加内容 doc.Blocks.Add(new Paragraph(new Run(TextContent)) { FontSize = 16, FontWeight = FontWeights.Bold, TextAlignment = TextAlignment.Center }); Section tableSection = new Section(); // 创建表格 Table table = new Table { CellSpacing = 0, //BorderBrush = Brushes.Black, //BorderThickness = new Thickness(1) BorderBrush = Brushes.Transparent, BorderThickness = new Thickness(0) }; // 添加列定义 table.Columns.Add(new TableColumn { Width = new GridLength(170 / 4) }); // 商品名称 table.Columns.Add(new TableColumn { Width = new GridLength(170 / 4) }); // 数量 table.Columns.Add(new TableColumn { Width = new GridLength(170 / 4) }); // 单价 table.Columns.Add(new TableColumn { Width = new GridLength(170 / 4) }); // 小计 // 添加表头行 TableRow headerRow = new TableRow(); headerRow.Cells.Add(CreateTableCell("商品名称", FontWeights.Normal)); headerRow.Cells.Add(CreateTableCell("数量", FontWeights.Normal)); headerRow.Cells.Add(CreateTableCell("单价", FontWeights.Normal)); headerRow.Cells.Add(CreateTableCell("小计", FontWeights.Normal)); table.RowGroups.Add(new TableRowGroup { Rows = { headerRow } }); // 添加商品行 TableRowGroup itemRows = new TableRowGroup(); for (int i = 0; i < 10; i++) { TableRow row = new TableRow(); row.Cells.Add(CreateTableCell("xx小面包")); row.Cells.Add(CreateTableCell("2")); row.Cells.Add(CreateTableCell("65.00")); row.Cells.Add(CreateTableCell("130.00")); itemRows.Rows.Add(row); } table.RowGroups.Add(itemRows); tableSection.Blocks.Add(table); doc.Blocks.Add(tableSection); // 合计 Paragraph totalAmount = new Paragraph(new Run("合计:89898.55")) { TextAlignment = TextAlignment.Right, FontSize = 10, }; doc.Blocks.Add(totalAmount); // 添加分隔线 //doc.Blocks.Add(new Paragraph(new Run(new string('=', 30))) //{ // TextAlignment = TextAlignment.Center //}); // 添加公司 Paragraph company = new Paragraph(new Run("by 夏秋初")) { TextAlignment = TextAlignment.Center, FontSize = 10, }; doc.Blocks.Add(company); // 添加页脚 Paragraph footer = new Paragraph(new Run(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))) { TextAlignment = TextAlignment.Center, FontSize = 10, }; doc.Blocks.Add(footer); return doc; } // 创建表格单元格的辅助方法 private static TableCell CreateTableCell(string text, FontWeight? fontWeight = null, TextAlignment alignment = TextAlignment.Left) { TableCell cell = new TableCell(new Paragraph(new Run(text))); cell.TextAlignment = alignment; cell.FontWeight = fontWeight ?? FontWeights.Normal; cell.BorderBrush = Brushes.Black; cell.BorderThickness = new Thickness(0, 0, 1, 1); cell.FontSize = 10; cell.TextAlignment = TextAlignment.Center; cell.BorderBrush = Brushes.Transparent; cell.BorderThickness = new Thickness(0); return cell; } } public class PrinterInfo { public string Name { get; set; } public LocalPrintServer localPrintServer { get; set; } public bool IsDefault { get; set; } } }
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18928835
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18928835
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。

浙公网安备 33010602011771号