Printing Reports Directly to the Printer
If you decide not to user the viewer control for previewing reports, you will have to write all the code for loading and printing reports. The following sections show you examples of how to instantiate report objects and send their output to the printer.
At this point in the book, the amount of code you need to write is still very limited and it is easy to understand. Once you get into Part II of this book, you’ll see that this code serves as the foundation for performing advanced runtime customization of your reports.
Printing Untyped Reports Directly
Printing Untyped reports without using the viewer requires a bit more coding than the examples you’ve seen so far. First you must declare and instantiate a ReportDocument object. The ReportDocument object has methods for loading the report into memory and printing it. Call the Load() method and pass it the report’s filename. Call PrintToPrinter() to send the report to the printer.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
MyReport.Load("Employee List.rpt")
MyReport.PrintToPrinter(1, False, 0, 0)
End Sub
This example loads the Employee List report in the MyReport object variable. Then it calls the PrintToPrinter() method to print a single copy of the report, including all pages.
Printing Strongly-Typed Reports Directly
Printing Strongly-Typed reports is a little easier than printing Untyped reports. With Strongly-Typed reports, the report’s class name is part of the project and can be instantiate directly. Unlike Untyped reports, you don’t have to use the generic ReportDocument class and you don’t have to worry about loading the report from a file. After instantiating the report object, call the PrintToPrinter() method to send it to the printer.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyReport As New Employee_List
MyReport.PrintToPrinter(1, False, 0, 0)
End Sub
This declares the object variable MyReport to be of type Employee_List and instantiates it. For your application, use the class name of the report that you want to print. The PrintToPrinter() method prints a single copy of the report.