How do I make PDF the default export option for a Crystal Report?

后端 未结 1 581
无人及你
无人及你 2021-01-19 04:33

I am working with CrystalDecisions.CrystalReports.Engine.ReportDocument in WinForms in Visual Studio 2008. Right now when the users click the export button the dialog defaul

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 05:07

    As of CR XI, the only way I know is to replace the export dialog with your own. You can add your own button to the CrystalReportViewer control and hide their export button.

    Here's vb.net code to replace the export button with your own button/eventhandler...

    Public Shared Sub SetCustomExportHandler(ByVal crv As CrystalDecisions.Windows.Forms.CrystalReportViewer, ByVal export_click_handler As EventHandler)
            For Each ctrl As Control In crv.Controls
                'find the toolstrip
                If TypeOf ctrl Is ToolStrip Then
                    Dim ts As ToolStrip = DirectCast(ctrl, ToolStrip)
    
                    For Each tsi As ToolStripItem In ts.Items
    
                        'find the export button by it's image index
                        If TypeOf tsi Is ToolStripButton AndAlso tsi.ImageIndex = 8 Then
    
                            'CRV export button
                            Dim crXb As ToolStripButton = DirectCast(tsi, ToolStripButton)
    
                            'clone the looks of the export button
                            Dim tsb As New ToolStripButton
                            With tsb
                                .Size = crXb.Size
                                .Padding = crXb.Padding
                                .Margin = crXb.Margin
                                .TextImageRelation = crXb.TextImageRelation
    
                                .Text = crXb.Text
                                .ToolTipText = crXb.ToolTipText
                                .ImageScaling = crXb.ImageScaling
                                .ImageAlign = crXb.ImageAlign
                                .ImageIndex = crXb.ImageIndex
                            End With
    
                            'insert custom button in it's place
                            ts.Items.Insert(0, tsb)
    
                            AddHandler tsb.Click, export_click_handler
    
                            Exit For
                        End If
                    Next
    
                    Exit For
                End If
            Next
    
            'hide the default export button
            crv.ShowExportButton = False
        End Sub
    

    Then in the click handler you'd show a customized SaveFileDialog and eventually call the ReportDocument.ExportToDisk method. This way you can set the dialog's title and filename to something useful and of course set the default export type.

    0 讨论(0)
提交回复
热议问题