Thursday, March 29, 2007

Converting ASP.NET to PDF


I probably wouldn't be wrong if I said that most developers every now and then are tasked to present specific data contained in a webpage as a PDF document. There are two ways to do it:

  1. using the functionality of CrystalReportViewer's toolbar - this toolbar provides a button, which does exactly that. However, to use this approach you would have to actually display the crystal report on the page.

  2. programmatically convert the Crystal Report object into a PDF document using the Crystal Reports API

Below is the code for the second approach:

Dim report As CrystalReport1 = New CrystalReport1
SqlDataAdapter1.Fill(DataSet11)
report.SetDataSource(DataSet11)
report.ExportToHttpResponse _
(ExportFormatType.PortableDocFormat, _
Response, _
False, _
"ExportedReport")


Tuesday, March 27, 2007

Crystal Reports with ASP.NET

When it comes to developing a report for an ASP.NET application, one might find that there is very little information available on how it's done using Crystal Reports.

Visual Studio 2003 comes with a built-in version of Crystal Reports (CR 9). Using that built-in version of Crystal Reports might save you some money, since it's a part of VS 2003, however, from the Crystal Reports design prospective it is much more convenient to use a stand-alone version of Crystal Reports.

The latest version of Crystal Reports at the time of writing this post is Crystal Reports XI, which provides some new features, not available in previous versions (see the www.businessobjects.com website for more info), so in my last development I used that version of Crystal Reports.

In order for the report to display as a webpage, VS 2003 provides CrystalReportViewer control. This is how it works:

Instantiate the DataSet object

Dim ds As New DataSet

ds = GetDataForReport() 'some method that retrieves data for the report

Instantiate the ReportDocument object:

Dim rpt As New ReportDocument

'set the ReportObject as the report you want to display
rpt = New CrystalReport1
'Define the datasource of your report
rpt.SetDataSource(ds)

'define your report as a reportsource for the crystalreportviewer
CrystalReportViewer1.ReportSource = rpt

Below is C# version of this code:

DataSet ds = new DataSet();
ReportDocument rpt = new ReportDocument();
rpt = new CrystalReport1();
rpt.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rpt;


In order for this code to work, the following namespaces must be imported/used:

CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Shared
CrystalDecisions.Web.Design

The following namespaces should be used only if you are planning to convert the report into a PDF, Word, or Excel format.

CrystalDecisions.Shared.ExportDestinationType
CrystalDecisions.Shared.ExportFormatType
CrystalDecisions.Shared.ExportDestinationOptions