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

C#使用ReportDocument打印报表时如何显示打印对话框供用户选任意打印机

你可以通过调用WinForm自带的PrintDialog组件实现打印对话框弹出,支持用户自主选择打印机、调整打印份数、页码范围等参数,修改后的实现逻辑如下:

操作步骤

  • 实例化PrintDialog对象,将你预设的打印参数作为初始配置传入
  • 调用ShowDialog()方法弹出打印选择窗口,判断用户是否点击确认按钮
  • 若用户确认打印,直接使用对话框返回的PrinterSettings参数调用报表打印方法

完整代码示例

var report = new ReportDocument();
report.Load(System.Windows.Forms.Application.StartupPath + @"\Reports\RouteSlip.rpt");
report.Database.Tables[0].SetDataSource(dtData.Tables[0]);       

// 初始化默认打印参数
var prntSettings = new PrinterSettings
{
  Copies = 1,
  Collate = false,
};

// 弹出打印对话框
var printDialog = new PrintDialog
{
    PrinterSettings = prntSettings,
    AllowSomePages = true, // 允许选择页码范围,不需要可以设为false
    AllowSelection = true // 允许打印选中内容,不需要可以设为false
};

// 用户确认打印后再执行打印操作
if (printDialog.ShowDialog() == DialogResult.OK)
{
    report.PrintToPrinter(printDialog.PrinterSettings, new PageSettings(), false);
}

注意事项

如果你的项目不是WinForm类型项目,需要先手动添加System.Windows.Forms程序集引用,再使用PrintDialog组件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 15:18:04