Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
saurabh_pathak
Active Contributor
0 Kudos

Purpose

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.


Overview

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.

Overload list
Description
PrintToPrinter(int nCopeis, bool collated, int startPageN, int endPageN)
  1. Prints the specified pages of the report to the printer selected using the PrintOptions.PrinterName property. If no printer is selected, the default printer specified in the report will be used.
  2. It uses the print options defined in the report or the PrintOptions API.
  3. PrintOptions API covers most of the basic settings for printing but not all.
PrintToPrinter(System.Drawing.Printing.PrinterSettings printerSettings, System.Drawing.Printing.PageSettings pageSettings, bool reformatReportPageSettings)
  1. Prints the report either to a printer or to a file, using the settings provided by the System.Drawing.Printing.PrinterSettings object and the System.Drawing.Printing.PageSettings object.
  2. System.Drawing.Printing Namespace gives more granular control over print job. It allows user to specify settings of a printer/driver.
  3. User can have custom paper sizes, access additional enums that are not in PrintOptions API and access other properties that might be specific to the printer being used (i.e. label printers, multi-tray printers etc...)
PrintToPrinter(System.Drawing.Printing.PrinterSettings printerSettings, System.Drawing.Printing.PageSettings pageSettings, bool reformatReportPageSettings, PringLayoutSettings PrintLayout )
  1. Prints the report either to a printer or to a file, using the settings provided by the System.Drawing.Printing.PrinterSettings object, the System.Drawing.Printing.PageSettings object and the CrystalDecisions.Shared.PrintLayoutSettings object.
  2. System.Drawing.Printing Namespace gives more granular control over print job. It allows user to specify settings of a printer/driver.
  3. User can have custom paper sizes, access additional enums that are not in PrintOptions API and access other properties that might be specific to the printer being used (i.e. label printers, multi-tray printers etc...)
  4. It gives access to PrintLayoutSettings class which allows control over scaling and positioning of the printed report.

Start Here

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.

Common Resolution

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.

Resolution 1

Follow the steps:

  1. Open the report in designer and go to File > Page Setups.
  2. 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.

Resolution 2

Use the following code for printing the reports:


Sample Code

ReportDocument rpt = new ReportDocument();      

rpt.Load(Server.MapPath("ReportName"));              

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);

Related Contents

Related Documents

Crystal Report printing inconsistancies in .NET

Server side printing with Crystal .NET SDK

How Printer Driver Options Affect a Report

Crystal Reports .NET SDK API Reference

Related Notes

1715391 - Crystal Reports PrintToPrinter API for .NET not responding on WIN 2008 Server

1729446 - The print job queuing becomes very slow when using custom paper size in Crystal Reports th...

1533800 - 'System.Runtime.InteropServices.COMException (0x80000201): Invalid printer specified' when...

1549797 - Printing differences between Crystal designer and .NET SDK

Labels in this area