• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2453
  • Last Modified:

CrystalReportViewer for ASP.NET prints the toolbar

I have read "How to Design a good Crystal Report" from ehanoi and I think the conclusion is that Crystal Report Viewer for .Net does not come with a print button and if the toolbar is shown then when you print the report the toolbar appears on the report. If you hide the toolbar then of course it won't show on the printed page but you cannot navigate the report. This is bad, but what is worse is that when you print a report such as letters for individuals, it will print only the one displayed on the browser. Please let me know if this is so and there is no work around it. I pretty much have exhausted all options
0
Yves Mellet
Asked:
Yves Mellet
1 Solution
 
kssaranCommented:
Yes, You are right. But you can think of alternative in this way. When the user wants to print the report why dont u export the same report to a PDF File in the run time and parse it the new browser. Then user can take the print out from the PDF File, which will have only the output of the report.

Try the following code

    Dim strInvoiceNo As String
    Dim strSelectFormula As String
    Dim ServerName As String, UserId As String, Password As String

    Private Sub cmdShowReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShowReport.Click
        ExportReport("servername", "username", "password")
    End Sub

    Private Sub ExportReport(ByVal ServerName As String, ByVal UserId As String, ByVal Password As String)
    'To Export the Result into a PDF File and display the Same in the window
        Dim myReport As New SampleReport()
        strInvoiceNo = txtInvoiceNo.Text
        strSelectFormula = "{invoice_header.inh_invoiceno} = '" + strInvoiceNo + "'"
        myReport.RecordSelectionFormula = strSelectFormula
        'Set the logon credentials of the main report
        LogonToDatabase(myReport.Database.Tables, ServerName, UserId, Password)

        'Modify the sub-report properties

        Dim mySection As CrystalDecisions.CrystalReports.Engine.Section
        Dim myReportObject As CrystalDecisions.CrystalReports.Engine.ReportObject
        Dim mySubReport As CrystalDecisions.CrystalReports.Engine.SubreportObject
        Dim mySubReportDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument

        'Loop through each section of the main report

        For Each mySection In myReport.ReportDefinition.Sections
            'Drill down into the section to see if there are any sub-reports
            For Each myReportObject In mySection.ReportObjects
                If myReportObject.Kind = _
                    CrystalDecisions.[Shared].ReportObjectKind.SubreportObject Then
                    mySubReport = CType(myReportObject, _
                        CrystalDecisions.CrystalReports.Engine.SubreportObject)

                    'Get a reference to the sub-report object
                    'so we can change its properties
                    mySubReportDoc = mySubReport.OpenSubreport(mySubReport.SubreportName)
                    'Set the logon credentials
                    LogonToDatabase(mySubReportDoc.Database.Tables, ServerName, UserId, Password)
                End If
            Next
        Next
        Dim myExportExt As String, myExportType As String, myExportFile As String
        Dim strExportType As String
        strExportType = "PDF"

        If strExportType = UCase("Excel") Then
            myExportExt = ".xls"
            myExportType = "application/vnd.ms-excel"
        ElseIf strExportType = UCase("HTML") Then
            myExportExt = ".html"
            myExportType = "application/html"
        ElseIf strExportType = UCase("PDF") Then
            myExportExt = ".pdf"
            myExportType = "application/pdf"
        ElseIf strExportType = UCase("RichText") Then
            myExportExt = ".rtf"
            myExportType = "application/rtf"
        ElseIf strExportType = UCase("Word") Then
            myExportExt = ".doc"
            myExportType = "application/msword"
        End If

        Dim myExportOptions As CrystalDecisions.Shared.ExportOptions
        Dim myDiskFileDestinationOptions As CrystalDecisions.Shared.DiskFileDestinationOptions

        'myExportFile = Server.MapPath(".") & "\" & Session.SessionID.ToString & myExportExt
        myExportFile = "c:\winnt\temp\" & Session.SessionID.ToString & myExportExt

        myDiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()
        myDiskFileDestinationOptions.DiskFileName = myExportFile
        myExportOptions = myReport.ExportOptions

        With myExportOptions

            .DestinationOptions = myDiskFileDestinationOptions
            .ExportDestinationType = .ExportDestinationType.DiskFile

            If strExportType = UCase("Excel") Then
                .ExportFormatType = .ExportFormatType.Excel
            ElseIf strExportType = UCase("HTML") Then
                .ExportFormatType = .ExportFormatType.HTML40
            ElseIf strExportType = UCase("PDF") Then
                .ExportFormatType = .ExportFormatType.PortableDocFormat
            ElseIf strExportType = UCase("RichText") Then
                .ExportFormatType = .ExportFormatType.RichText
            ElseIf strExportType = UCase("Word") Then
                .ExportFormatType = .ExportFormatType.WordForWindows
            End If
        End With

        'Exports the report to PDF format
        myReport.Export()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = myExportType
        Response.WriteFile(myExportFile)
        Response.Flush()
        Response.Close()

        System.IO.File.Delete(myExportFile)
    End Sub

    Private Sub LogonToDatabase(ByVal ReportTables As CrystalDecisions.CrystalReports.Engine.Tables, ByVal ServerName As String, ByVal UserId As String, ByVal Password As String)
    ' To Supply Logon Information to each and every Tables used in the Report
        Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim myConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo()
        Dim myLogonInfo As New CrystalDecisions.Shared.TableLogOnInfo()
        myConnectionInfo.UserID = UserId
        myConnectionInfo.Password = Password
        myConnectionInfo.ServerName = ServerName
        myLogonInfo.ConnectionInfo = myConnectionInfo
        For Each myTable In ReportTables
            myTable.ApplyLogOnInfo(myLogonInfo)
        Next
    End Sub

End Class


0
 
EwaldLCommented:
you get a print button on the web viewer if you upgrade to cr 9 or 10 developer or advanced.
0
 
Yves MelletAuthor Commented:
Thanks for the help kssaran and Ewaldl. Particularly you kssaran for sending me all that code. It worked!
I would also like to follow Ewaldl advice to upgrade to cr9. I have the Advance edition but it  doesn't say it is for .net.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now