Monday, August 31, 2009

How programmatically set Custom Page Size while exporting Crystal Report using VB.NET

How programmatically set Custom Page Size while exporting Crystal Report using VB.NET


You can easily set paper size with export functionality. Simply design crystal report as you do and bind it with database, dataset or any other data source.


When you export crystal report its takes paper size based on the crystal report design page size, if your requirement to display or print exported file in particular format then following code will help to do this.


Following example tested in


1) .NET Framework 2.0 and Visual Studio 2005

2) Crystal Report with Visual Studio 2005


DLL references requires


VB.NET Code


'Fill dataset as with desired data needs to populates crystal report
Dim dsReportOut As New DataSet
Dim rptDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim reportPath As String = "CrystalReportFilePath"
Dim fileName As String = "ExportFileName.doc"
'Build the report logic here. Declare a report document.
rptDoc = New CrystalDecisions.CrystalReports.Engine.ReportDocument()
rptDoc.Load(String.Concat(reportPath, "CrystalReportFileName.rpt"))
rptDoc.SetDataSource(dsReportOut)
'Set Paper Size as A4 its letter size
rptDoc.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperLetter
--**(See below for other PaperSize options)
'Export crystal report into MSWord
rptDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.WordForWindows, String.Concat(reportPath, fileName))
--***(See below for other Export Format Options)
'Open(Exported file)MSWord File
Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo
ProcessStartInfo.FileName = String.Concat(reportPath, fileName)
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Maximized
System.Diagnostics.Process.Start(ProcessStartInfo)

** Others PaperSize Options


*** Others Export Options