Troubleshooting issues related to PrintToPrinter() when used in .NET applications for printing Crystal Reports
The purpose of this document is to provide the ways to troubleshoot the issues related to PrintToPrinter() when used in .NET applications for printing Crystal Reports.
PrintToPrinter() is either used to the print the report to a printer or to a file. It has 3 overloads that can be used depending upon the requirement.
|PrintToPrinter(int nCopeis, bool collated, int startPageN, int endPageN)||
|PrintToPrinter(System.Drawing.Printing.PrinterSettings printerSettings, System.Drawing.Printing.PageSettings pageSettings, bool reformatReportPageSettings)||
|PrintToPrinter(System.Drawing.Printing.PrinterSettings printerSettings, System.Drawing.Printing.PageSettings pageSettings, bool reformatReportPageSettings, PringLayoutSettings PrintLayout )||
While printing the report from the application using PrintToPrinter(), some of the problems are often seen with scaling, orientation, customer paper sizes, fonts, spacing driver specific or driver access, tray access, not able to set the printer when a network printer is used, and when output goes to a default printer rather than desired printer etc… Some of the known errors are:
- No Default printer
- Invalid printer specified
- The default printer is not valid. Printing will be disabled.
- Settings to access printer <printername> are not valid
This document would focus on PrintToPrinter(System.Drawing.Printing.PrinterSettings printerSettings, System.Drawing.Printing.PageSettings pageSettings, bool reformatReportPageSettings, PringLayoutSettings PrintLayout ) and the the reason for using it is that; System.Drawing.Printing Namespace gives more granular control over PaperSize, PaperSource, Margins, PageSettings etc… than the PrintOptions settings used when using the first overload for PrintToPrinter() moreover CrystalDecisions.Shared.PrintLayoutSettings class helps specify custom layout options.
As per the scenarios given above, following resolutions work most of the times. However, it should be ensured that printer is installed on the machine where application is deployed and drivers are updated to the latest. User/identity/process should have access to the printer. To check if the user/ identity/process running the application has rights to access the printer, implement the code given in KB1418378 – How to determine what printers are available to a process in Visual Studio .NET. The code given in the above knowledge base article is most useful in web applications where the process executing the report is running under a different user context, though it can also prove useful when troubleshooting thick client applications.
Follow the steps:
- Open the report in designer and go to File > Page Setups.
- Select No Printer and Dissociate Formatting Page Size and Printer Paper Size.
If selecting No Printer is not an option then go for the code given below.
Use the following code for printing the reports:
ReportDocument rpt = new ReportDocument();
System.Drawing.Printing.PrintDocument pDoc = new System.Drawing.Printing.PrintDocument();
CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = “PinterName”;
System.Drawing.Printing.PageSettings pSettings = new System.Drawing.Printing.PageSettings(printerSettings);
rpt.PrintOptions.DissociatePageSizeAndPrinterPaperSize = true;
rpt.PrintOptions.PrinterDuplex = PrinterDuplex.Simplex;
rpt.PrintToPrinter(printerSettings, pSettings, false, PrintLayout);