I am working on Crystal Reports for Visual Studio 2005. I need to change the default printer, and the number of copies to 2 as compared to the default of 1.
I have succeeded to change the default printer using below code.
static int SetAsDefaultPrinter(string printerDevice)
{
int ret = 0;
try
{
string path = "win32_printer.DeviceId='" + printerDevice + "'";
using (ManagementObject printer = new ManagementObject(path))
{
ManagementBaseObject outParams =
printer.InvokeMethod("SetDefaultPrinter",
null, null);
ret = (int)(uint)outParams.Properties["ReturnValue"].Value;
}
}
}
How can I change the number of copies printed?
.Net Framework doesn't provide any mechanism to override the default print functionality. So I disabled the default print button, and added a button name Print.Code for the Event Handler follows below.
private void Print_Click(object sender, EventArgs e)
{
try
{
PrintDialog printDialog1 = new PrintDialog();
PrintDocument pd = new PrintDocument();
printDialog1.Document = pd;
printDialog1.ShowNetwork = true;
printDialog1.AllowSomePages = true;
printDialog1.AllowSelection = false;
printDialog1.AllowCurrentPage = false;
printDialog1.PrinterSettings.Copies = (short)this.CopiesToPrint;
printDialog1.PrinterSettings.PrinterName = this.PrinterToPrint;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
PrintReport(pd);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void PrintReport(PrintDocument pd)
{
ReportDocument rDoc=(ReportDocument)crvReport.ReportSource;
// This line helps, in case user selects a different printer
// other than the default selected.
rDoc.PrintOptions.PrinterName = pd.PrinterSettings.PrinterName;
// In place of Frompage and ToPage put 0,0 to print all pages,
// however in that case user wont be able to choose selection.
rDoc.PrintToPrinter(pd.PrinterSettings.Copies, false, pd.PrinterSettings.FromPage,
pd.PrinterSettings.ToPage);
}