Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

19.11 Printing via the Browser

Printing with ASP.NET

The Crystal Reports web viewer control gives you a print button to send your reports directly to the printer. But, there are times when you want to view them in a different format, or save them to a separate file. For example, many people like to view reports as a PDF file because they feel that it gives the report a more professional look.

The ReportDocument.ExportToHttpResponse() method exports files directly to the Http Response object. This lets the user view the file directly in the browser window or save it to their local computer.

The code for using the ExportToHttpResponse() method is very similar to calling the Export() method. The primary difference is that you don’t need to specify the destination option because it is always being exported to Http. Second, there are a couple new arguments to pass to it. The method is overloaded with two declarations:

ExportToHttpResponse(ExportFormatType, HttpResponse, Attachment (Boolean), Filename)
ExportToHttpResponse(ExportOptions, HttpResponse, Attachment (Boolean), Filename)

The first declaration is the more simplistic of the two. The first parameter is the format type (PDF, Excel, etc.). The second parameter is the System.Web.HttpResponse object. It is used to stream the file directly to the browser. The third parameter is a Boolean that specifies if the file should be a separate attachment or not. If this is True, the user is prompted with the File Download dialog box. The fourth parameter is the default filename presented to the user is when they save the file. Do not use a file extension with the filename because this is done automatically depending upon the file type.

The second method declaration is more complex because the first parameter expects an ExportOptions object. This allows you to create a FormatType object specific to the file type and set its properties. For example, a PDF file can specify the page range to print. You can’t do this when using the first declaration.

The normal method of viewing an ASP.NET report is very efficient because pages are generated as they are displayed and this conserves resources. This isn’t the case when exporting to a file. The entire report has to be generated to create the file and it has to be sent to the user’s browser to be displayed. For large reports, your users might have a longer than normal delay.

The following two listings show an example of how to call each method declaration. The first, Listing 19-6, performs a simple export to a PDF file. The second listing, 19-7, shows how to export using an ExportOptions object and it only prints the first three pages of the report.

To thoroughly illustrate the both export options available to you, the VB.NET code exports directly to the browser window. The C# code exports the file as an attachment and saves it to the file “EmployeeList.pdf”

Listing 19-6. Export a file using the ExportFormatType enumeration.
[VB.NET]
Public Sub ExportToPdfHttp()
Dim myReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
myReport.Load(Server.MapPath("EmployeeList.rpt"))
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
myReport.ExportToHttpResponse( CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, False, "")
End Sub
Class [C#]
public void ExportToPdfHttp()
{
CrystalDecisions.CrystalReports.Engine.ReportDocument myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myReport.Load(Server.MapPath("EmployeeList.rpt"));
myReport.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "EmployeeList");
}
Listing 19-7. Export a file using an ExportOptions object.
[VB.NET]
Public Sub ExportToPdfHttp()
Dim myReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
myReport.Load(Server.MapPath("EmployeeList.rpt"))
Dim myExportOptions As New CrystalDecisions.Shared.ExportOptions
Dim myFormatOptions As CrystalDecisions.Shared.PdfRtfWordFormatOptions
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreatePdfRtfWordFormatOptions()
myFormatOptions.UsePageRange = True
myFormatOptions.FirstPageNumber = 1
myFormatOptions.LastPageNumber = 3
myExportOptions.ExportFormatOptions = myFormatOptions
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
myReport.ExportToHttpResponse(myExportOptions, Response, False, "")
End Sub
Class [C#]
public void ExportToPdfHttp()
{
CrystalDecisions.CrystalReports.Engine.ReportDocument myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myReport.Load(Server.MapPath("EmployeeList.rpt"));
CrystalDecisions.Shared.ExportOptions myExportOptions = new CrystalDecisions.Shared.ExportOptions();
myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
CrystalDecisions.Shared.PdfRtfWordFormatOptions myFormatOptions;
myFormatOptions = CrystalDecisions.Shared.ExportOptions.CreatePdfRtfWordFormatOptions();
myFormatOptions.UsePageRange = true;
myFormatOptions.FirstPageNumber = 1;
myFormatOptions.LastPageNumber = 3;
myExportOptions.FormatOptions = myFormatOptions;
myReport.ExportToHttpResponse(myExportOptions, Response, true, "EmployeeList");
}