65.9K
CodeProject is changing. Read more.
Home

Reading RPT File Then Display Report as PDF

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.73/5 (6 votes)

Oct 24, 2005

3 min read

viewsIcon

47310

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.

 

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

 

    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)

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)

        'For Educational Purpose Only

        'For Explanation of this function, see my previous article

        With SourceReport.FormatEngine.PrintOptions

            .PaperSize = PaperSize

            .PaperOrientation = PaperOrientation

        End With

        Dim st As System.IO.Stream

        Dim b() As Byte

        st = SourceReport.ExportToStream(FormatType)

        Page.Response.ClearHeaders()

        Page.Response.ContentType = "application/pdf"

        ReDim b(st.Length)

        st.Read(b, 0, CInt(st.Length))

        Page.Response.BinaryWrite(b)

        Page.Response.End()

    End Function

 

Next, we call the function when page loaded.

 

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

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)

        'For Educational Purpose Only

        'For Explanation of this function, see my previous article

        Dim myRpt As New ReportDocument

        myRpt.Site = Me.Site

        Try

            myRpt.Load(MapPath("../RPTFiles/Training.Rpt"))

            PrintDocument(myRpt, CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat, CrystalDecisions.[Shared].PaperSize.PaperLetter, CrystalDecisions.[Shared].PaperOrientation.Portrait)

        Catch ex As Exception

            Exit Sub

        End Try

    End Sub

 

 

The Complete Source code are :

 

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.ReportSource

Imports CrystalDecisions.Shared

 

Public Class ReportTraining

    Inherits System.Web.UI.Page

 

#Region " Web Form Designer Generated Code "

 

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

 

    End Sub

 

    'NOTE: The following placeholder declaration is required by the Web Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

 

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        'CODEGEN: This method call is required by the Web Form Designer

        'Do not modify it using the code editor.

        InitializeComponent()

    End Sub

 

#End Region

 

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

        'Put user code to initialize the page here

        Dim myRpt As New ReportDocument

        myRpt.Site = Me.Site

        Try

            myRpt.Load(Me.MapPath("../CPXPrint/Training.Rpt"))

            PrintDocument(myRpt, CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat, CrystalDecisions.[Shared].PaperSize.PaperLetter, CrystalDecisions.[Shared].PaperOrientation.Portrait)

        Catch ex As Exception

            Exit Sub

        End Try

    End Sub

 

    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)

        'Copyright IMAWA 2005 (Bluechip_Asia@yahoo.com)

        'For Educational Purpose Only

        'For Explanation of this function, see my previous article

        With SourceReport.FormatEngine.PrintOptions

            .PaperSize = PaperSize

            .PaperOrientation = PaperOrientation

        End With

        Dim st As System.IO.Stream

        Dim b() As Byte

        st = SourceReport.ExportToStream(FormatType)

        Page.Response.ClearHeaders()

        Page.Response.ContentType = "application/pdf"

        ReDim b(st.Length)

        st.Read(b, 0, CInt(st.Length))

        Page.Response.BinaryWrite(b)

        Page.Response.End()

    End Function

End Class