Thursday, May 10, 2007

Crystal Reports Export to PDF

Well, I finally did it. The code below is what lets me export a crystal report to a pdf file. Previous entries outline some of the steps I have taken to get here. Not sure if all were necessary but it works. No I have to figure out how to pass parameters, datasource and login information. On to figure that out.

Option Strict On
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class _Default
Inherits System.Web.UI.Page
Private exportPath As String
Private myDiskFileDestinationOptions As DiskFileDestinationOptions
Private myExportOptions As ExportOptions
Private myReport As ReportDocument
Private selectedNoFormat As Boolean = False
Private Sub ConfigureCrystalReports()
myReport = New ReportDocument()
myReport.Load("C:\websites\rpt\report1.rpt")
'myCrystalReportViewer.ReportSource = myReport
End Sub

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
ConfigureCrystalReports()
ExportSetup()
myExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
myDiskFileDestinationOptions.DiskFileName = exportPath & "PortableDoc.pdf"
myExportOptions.DestinationOptions = myDiskFileDestinationOptions
myReport.Export()
End Sub
Public Sub ExportSetup()
exportPath = "C:\Exported\"
If Not System.IO.Directory.Exists(exportPath) Then
System.IO.Directory.CreateDirectory(exportPath)
End If
myDiskFileDestinationOptions = New DiskFileDestinationOptions()
myExportOptions = myReport.ExportOptions
myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
End Sub
End Class

No comments: