Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

19.16 Printing with ASP.NET

Printing with ASP.NET

Printing a report from a browser gives you a less than professional report. The browser can’t differentiate between what is part of the report and the other HTML objects shown on the web page (e.g. the navigation buttons). It prints everything. Unfortunately, the web version of the viewer control doesn’t have a print button that can be displayed on your form. The workaround for this problem is to export the report to a PDF file and then load the PDF file into the browser. 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 PDF file. The entire report has to be generated to create the file and then 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. Listing 19-8 shows a generic method for printing reports. Pass it a report object (you have to declare and instantiate the report object prior to calling this method) and a filename to export the PDF file to. Listing 19-8. Print from ASP.NET using a PDF file.
Public Sub PrintToPdfWithFile(ByVal MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal FileName As String)
'Instantiate the object that controls where the file is exported to
Dim DestOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions
FileName = Request.PhysicalApplicationPath & Session.SessionID & "_" & _
FileName
DestOptions.DiskFileName = FileName
'Set the Export Options
MyReport.ExportOptions.ExportFormatType = _
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
MyReport.ExportOptions.ExportDestinationType = _
CrystalDecisions.Shared.ExportDestinationType.DiskFile
MyReport.ExportOptions.DestinationOptions = DestOptions
MyReport.Export()
'Display the PDF file in the current browser window
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile(FileName)
Response.Flush()
Response.Close()
System.IO.File.Delete(FileName)
End Sub The first step instantiates a DiskFileDestinationOptions object that stores the destination PDF file’s fully qualified file path. Notice that the FileName was modified so that it points to the local folder and also concatenates the Session Id with the name. This insures that the same report can be exported for different users without overwriting each other. The second step sets the export options of the ReportDocument object. This tells it to use the PDF format and that it is a disk file. It also assigns the DestinationOption property so that it knows where to save the file. Call the Export() method to save the report to the PDF file. After the file is exported, you have to display it to the user. The last set of steps uses the Response object to load the PDF file and stream it to the browser window. Once the file has been exported and streamed to the browser, the temporary output file is deleted. It is very common for you to get an error stating that the temporary report file is not accessible. This is a confusing error message because it is written to make you think that you can’t load the actual report file. But this isn’t the case. What the error message should have said is that you do not have write permissions to the directory where you are storing the export file. You need to make sure that the ASPNET user has proper permissions to the folder it is storing the PDF file to. Many times people insist this isn’t the problem only to find out that it is. A simple way to test if this is your problem is to write the PDF file to the folder C:\Windows\Temp. The ASPNET user has full access to this folder by default. If you make this change and your report exports with no errors, then you know that you need to set the proper permissions on the output folder.If you don’t want to deal with the overhead of creating temporary export files and deleting them afterwards, export the report to a binary stream that is sent directly to the browser. This skips the intermediate step of writing the file to disk and later deleting it. Listing 19-9 shows the code for this.

Listing 19-9. Export to PDF using a binary stream.
Public Sub PrintToPdfWithStream(ByVal MyReport As _
CrystalDecisions.CrystalReports.Engine.ReportDocument)
Dim MyExportOptions As New CrystalDecisions.Shared.ExportOptions
MyExportOptions.ExportFormatType = _
CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
Dim MyExportRequestContext As _
New CrystalDecisions.Shared.ExportRequestContext
MyExportRequestContext.ExportInfo = MyExportOptions
Dim MyStream As System.IO.Stream
MyStream = MyReport.FormatEngine.ExportToStream(MyExportRequestContext)
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "application/pdf"
Dim MyBuffer(MyStream.Length) As Byte
MyStream.Read(MyBuffer, 0, CType(MyStream.Length, Integer))
Response.BinaryWrite(MyBuffer)
Response.End()
End Sub

The previous two code samples exported to a PDF document. This is because Adobe Acrobat PDF files have become the defacto standard for displaying documents on the internet. But that doesn’t mean that don’t have any other options. You can export to a variety of formats. Change the ExportFormatType property and the Response.ContentType property. Table 19-11 shows the constants for setting the Response.ContentType property.

Table 19-11. String constants for the Response.ContentType property

Export Type String Constant
Rich Text Format (.RTF) “application/msword”
Adobe Acrobat (.PDF) “application/pdf”
MS Word (.DOC) “application/msword”
MS Excel (.XLS) “application/vnd.ms-excel”
Crystal Report (.RPT) “application/x-rpt”