Printing with ASP.NET
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
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” |