Share via


Tutorial: Printing and Setting Print Options

Introduction

You can configure print options and call a printer from code, rather than from the Crystal Reports UI. To do that, you use the PrintOptions class and the PrintToPrinter() method of ReportDocument object model.

If the Print button on the toolbar of the CrystalReportViewer control meets your printing needs, then you do not need to write code to configure additional print options.

However, a code-based approach to printing reports is useful in specialized scenarios:

  • You can control when, where, and how printing occurs. To do that, you disable the Print button in the toolbar of the CrystalReportViewer control and manage all printing through code.

  • You can print a report in the background, without displaying it. All of the Print settings are contained within the ReportDocument model, which can be instantiated and configured without ever displaying the report with a CrystalReportViewer control.

  • You can centralize all printing on the Web server for a Web client. Use the PrintToPrinter() method to send print jobs to a printer that is connected to the Web server, rather than send print jobs to a local printer that is connected to the Web client.

    Note

    This back-end printing functionality is less extensive than the report-scheduling framework that is provided with Crystal Reports Server or BusinessObjects Enterprise. For more information about these products, see Upgrade Options.

To begin this tutorial, you add several list controls to set print options above the CrystalReportViewer control on your Web or Windows Form.

Then, in the code-behind class, you create a CURRENT_PRINTER string constant, to which you assign the path of the printer that you want to use.

Next, you bind each list control to an enumeration that contains print options for paper orientation, paper size, and printer duplex settings. An additional list control displays custom paper source settings for the printer that is currently designated in the CURRENT_PRINTER constant. For the custom paper source control, you create a helper method that instantiates PrinterSettings, assigns it to the current printer, and then returns an array of paper sources for that printer.

Then you create a button click event method for the Print Report button. In that event method, each print option property is assigned a value that is based on selections made in the list controls. Finally, the report is printed to the printer that is designated in the CURRENT_PRINTER constant.

Sample Code

This tutorial comes with Visual Basic and C# sample code that show the completed version of the project. Follow the instructions in this tutorial to create a new project or open the sample code project to work from a completed version.

The sample code is stored in folders that are categorized by language and project type. The folder names for each sample code version are as follows:

  • C# Web Site: CS_Web_RDObjMod_SetPrintOptions
  • C# Windows project: CS_Win_RDObjMod_SetPrintOptions
  • Visual Basic Web Site: VB_Web_RDObjMod_SetPrintOptions
  • Visual Basic Windows project: VB_Win_RDObjMod_SetPrintOptions

To locate the folders that contain these samples, see Tutorials' Sample Code Directory.

In this section: