Click here to Skip to main content
15,892,005 members
Articles / Web Development / ASP.NET
Article

Reading RPT File Then Display Report as PDF

Rate me:
Please Sign up or sign in to vote.
1.73/5 (6 votes)
23 Oct 20053 min read 47.2K   27   1
This Article Teach You How To Use External RPT File Rather Than Embedded RPT File (.DLL)

Introduction

When we develop a report for web application, the RPT file usually embedded into an DLL file. So when we want to edit the report, we must recompile the project. This is the simple solution for reading RPT file then displays it as PDF.

 <o:p>

Now let’s creating a function to export and display your report into PDF.

 <o:p>

    Private Function PrintDocument(ByVal SourceReport As Object, ByVal FormatType As CrystalDecisions.Shared.ExportFormatType, ByVal PaperSize As CrystalDecisions.Shared.PaperSize, ByVal PaperOrientation As CrystalDecisions.Shared.PaperOrientation)<o:p>

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)<o:p>

        'For Educational Purpose Only<o:p>

        'For Explanation of this function, see my previous article<o:p>

        With SourceReport.FormatEngine.PrintOptions<o:p>

            .PaperSize = PaperSize<o:p>

            .PaperOrientation = PaperOrientation<o:p>

        End With<o:p>

        Dim st As System.IO.Stream<o:p>

        Dim b() As Byte<o:p>

        st = SourceReport.ExportToStream(FormatType)<o:p>

        Page.Response.ClearHeaders()<o:p>

        Page.Response.ContentType = "application/pdf"<o:p>

        ReDim b(st.Length)<o:p>

        st.Read(b, 0, CInt(st.Length))<o:p>

        Page.Response.BinaryWrite(b)<o:p>

        Page.Response.End()<o:p>

    End Function

 <o:p>

Next, we call the function when page loaded.

 <o:p>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)<o:p>

        'For Educational Purpose Only<o:p>

        'For Explanation of this function, see my previous article<o:p>

        Dim myRpt As New ReportDocument<o:p>

        myRpt.Site = Me.Site<o:p>

        Try<o:p>

            myRpt.Load(MapPath("../RPTFiles/Training.Rpt"))<o:p>

            PrintDocument(myRpt, CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat, CrystalDecisions.[Shared].PaperSize.PaperLetter, CrystalDecisions.[Shared].PaperOrientation.Portrait)<o:p>

        Catch ex As Exception<o:p>

            Exit Sub<o:p>

        End Try<o:p>

    End Sub<o:p>

 <o:p>

 <o:p>

The Complete Source code are :<o:p>

 <o:p>

Imports CrystalDecisions.CrystalReports.Engine<o:p>

Imports CrystalDecisions.ReportSource<o:p>

Imports CrystalDecisions.Shared<o:p>

 <o:p>

Public Class ReportTraining<o:p>

    Inherits System.Web.UI.Page<o:p>

 <o:p>

#Region " Web Form Designer Generated Code "<o:p>

 <o:p>

    'This call is required by the Web Form Designer.<o:p>

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()<o:p>

 <o:p>

    End Sub<o:p>

 <o:p>

    'NOTE: The following placeholder declaration is required by the Web Form Designer.<o:p>

    'Do not delete or move it.<o:p>

    Private designerPlaceholderDeclaration As System.Object<o:p>

 <o:p>

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<o:p>

        'CODEGEN: This method call is required by the Web Form Designer<o:p>

        'Do not modify it using the code editor.<o:p>

        InitializeComponent()<o:p>

    End Sub<o:p>

 <o:p>

#End Region<o:p>

 <o:p>

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<o:p>

        'Put user code to initialize the page here<o:p>

        Dim myRpt As New ReportDocument<o:p>

        myRpt.Site = Me.Site<o:p>

        Try<o:p>

            myRpt.Load(Me.MapPath("../CPXPrint/Training.Rpt"))<o:p>

            PrintDocument(myRpt, CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat, CrystalDecisions.[Shared].PaperSize.PaperLetter, CrystalDecisions.[Shared].PaperOrientation.Portrait)<o:p>

        Catch ex As Exception<o:p>

            Exit Sub<o:p>

        End Try<o:p>

    End Sub<o:p>

 <o:p>

    Private Function PrintDocument(ByVal SourceReport As Object, ByVal FormatType As CrystalDecisions.Shared.ExportFormatType, ByVal PaperSize As CrystalDecisions.Shared.PaperSize, ByVal PaperOrientation As CrystalDecisions.Shared.PaperOrientation)<o:p>

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)<o:p>

        'For Educational Purpose Only<o:p>

        'For Explanation of this function, see my previous article<o:p>

        With SourceReport.FormatEngine.PrintOptions<o:p>

            .PaperSize = PaperSize<o:p>

            .PaperOrientation = PaperOrientation<o:p>

        End With<o:p>

        Dim st As System.IO.Stream<o:p>

        Dim b() As Byte<o:p>

        st = SourceReport.ExportToStream(FormatType)<o:p>

        Page.Response.ClearHeaders()<o:p>

        Page.Response.ContentType = "application/pdf"<o:p>

        ReDim b(st.Length)<o:p>

        st.Read(b, 0, CInt(st.Length))<o:p>

        Page.Response.BinaryWrite(b)<o:p>

        Page.Response.End()<o:p>

    End Function<o:p>

End Class

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Indonesia Indonesia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThread was being aborted Pin
marquito_cuba5-Jun-06 11:44
marquito_cuba5-Jun-06 11:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.