在 C# 中使用 Crystal Reports 打印时选择打印机






2.98/5 (24投票s)
2005年12月7日
1分钟阅读

227558

8864
如何在 C# 中从 Crystal Reports 打印时选择打印机。
引言
我的项目中使用 Crystal Reports 9。我曾经遇到过一个打印报告的问题。然后,我在互联网上搜索并找到了来自 这个网站 的一段代码,它向我展示了如何在不预览的情况下将报告发送到打印机,但这段代码不允许我选择打印机。 在本文中,我对代码进行了自定义,以使用打印对话框来选择打印机。
Using the Code
我使用打印对话框获取打印机名称。然后,我将其分配给报告。要打印报告,我使用该方法
crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);
将报告中的数据发送到打印机。
以下代码展示了如何选择打印机来打印报告
private void button2_Click(object sender, System.EventArgs e)
{
//Open the PrintDialog
this.printDialog1.Document = this.printDocument1;
DialogResult dr = this.printDialog1.ShowDialog();
if(dr == DialogResult.OK)
{
//Get the Copy times
int nCopy = this.printDocument1.PrinterSettings.Copies;
//Get the number of Start Page
int sPage = this.printDocument1.PrinterSettings.FromPage;
//Get the number of End Page
int ePage = this.printDocument1.PrinterSettings.ToPage;
//Get the printer name
string PrinterName = this.printDocument1.PrinterSettings.PrinterName;
crReportDocument = new ReportDocument();
//Create an instance of a report
crReportDocument = new Chart();
try
{
//Set the printer name to print the report to. By default the sample
//report does not have a default printer specified. This will tell the
//engine to use the specified printer to print the report. Print out
//a test page (from Printer properties) to get the correct value.
crReportDocument.PrintOptions.PrinterName = PrinterName;
//Start the printing process. Provide details of the print job
//using the arguments.
crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);
//Let the user know that the print job is completed
MessageBox.Show("Report finished printing!");
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
关注点
我认为本文最重要的部分是它展示了如何获取计算机或本地网络中的打印机名称。
许可证
本文没有明确的许可证附加到它,但可能包含在文章文本或下载文件中本身的使用条款。如有疑问,请通过下面的讨论区联系作者。作者可能使用的许可证列表可以在 这里 找到。