Introduction
I use Crystal Reports 9 in my project. I once encountered an issue with printing a report. Then, I searched the Internet and found a code from this website that showed me how to send a report to the printer without previewing it, but the code did not allow me to choose the printer. In this article, I have customized the code to use a Print Dialog to choose the printer.
Using the Code
I use the Print Dialog to get the printer name. Then, I assign it to the report. To print the report, I use the method:
crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);
to send the data from the report to the printer.
The code below shows how to choose the printer to print the report:
private void button2_Click(object sender, System.EventArgs e)
{
this.printDialog1.Document = this.printDocument1;
DialogResult dr = this.printDialog1.ShowDialog();
if(dr == DialogResult.OK)
{
int nCopy = this.printDocument1.PrinterSettings.Copies;
int sPage = this.printDocument1.PrinterSettings.FromPage;
int ePage = this.printDocument1.PrinterSettings.ToPage;
string PrinterName = this.printDocument1.PrinterSettings.PrinterName;
crReportDocument = new ReportDocument();
crReportDocument = new Chart();
try
{
crReportDocument.PrintOptions.PrinterName = PrinterName;
crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);
MessageBox.Show("Report finished printing!");
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
Points of Interest
I think the important thing in this article is it shows how to get the names of printers in your computer or your local network.
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.