Use ASP.NET to launch a report as a .pdf

Output a report at runtime that launches a new window with a .PDF version of the report.

Please let other users know how useful this tip is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.


This tip shows how to output a report at runtime that launches a new window with a .PDF version of the report.

This example assumes you have a .NET report named ReportFile included in the ASP.NET project. Create a blank ASP.NET form and add the data objects (data adapter, connection, dataset) to match the report file. Then set the page.load event to the following:


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
        'Populate the data source of the report.
        Me.daDataAdapter.Fill(Me.dsDataSet1)
        Dim crReport As New ReportFile
        crReport.SetDataSource(dsDataSet1)
        'Change the output stream of the current form
        Dim strStream As New System.IO.BinaryReader(crReport.ExportToStream
(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat))

        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(strStream.ReadBytes(strStream.BaseStream.Length))
        Response.Flush()
        Response.Close()

    End Sub

By redirecting to this form, it will populate the dataset, load the report as a .PDF and redirect that output stream to the client.

To have it launch in its own window, open the form with JavaScript. Often, I will have a form with a repeater control to display data from a database, and at the bottom, I'll have a button to display a printable version. That button opens the report Web form in a new window:


<INPUT onclick="javascript:window.open('PrintPage.aspx')" type="button"
value="View Printable Version">

If you need to supply a parameter, open the page with a querystring and read that value in the page.load event. Use a parameter item for the DataAdapter.Fill:


<INPUT onclick="javascript:window.open('PrintPage.aspx?Param=Hello')" 
type="button" value="View Printable Version">

PrintPage.aspx page.load event:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        'Get parameter value from querystring
        Dim tcParam As String
        tcParam = Me.Request.QueryString.Get("Param")
        Me.daDataAdapter.SelectCommand.Parameters("@tValue").Value = tcParam
        Me.daDataAdapter.Fill(Me.dsDataSet1)
        Dim crReport As New ReportFile
        crReport.SetDataSource(dsDataSet1)
        Dim strStream As New System.IO.BinaryReader(crReport.ExportToStream
(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat)

        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(strStream.ReadBytes(strStream.BaseStream.Length))
        Response.Flush()
        Response.Close()
    End Sub

I hope this helps spark some new ideas for using reports in ASP.NET. Thanks!

This was first published in March 2004

Dig deeper on Win Development Resources

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close