SIGN UP MEMBER LOGIN:    
ARTICLE

Tip: How to Export a Crystal Report to a PDF file Programmatically in ASP.NET?

Posted by Mahesh Chand Articles | Crystal Reports C# October 23, 2006
You can export a Crystal Report to various formats using CrystalReportViewer’s Export option. However, there are occasions when you want your users to see the report direct in PDF format in the browser as soon they click the Report button in your application.
Reader Level:

You can export a Crystal Report to various formats using CrystalReportViewer's Export option. However, there are occasions when you want your users to see the report direct in PDF format in the browser as soon they click the Report button in your application.

 

The ReportDocument class provides Export methods to export a report to various formats including PDF, Excel, Word, and HTML.

 

The following steps will guide you to achieve the same:

 

  1. Add crystal report (.cr) file to your ASP.NET application.
  2. Add a report instance on the page level.

    Dim report As MyReport = New MyReport

 

  1. Populate reports data on Page_Init  

     
    ' Get data in a DataSet or DataTable

        Dim ds As DataSet = GetData()

        ' Fill report with the data

     report.SetDataSource(ds)

 

  1. Export Report
    report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "ExportedReport")

 

If you wish to format report to other formats, just change the ExportFormatType enumeration value to your desired format.

 

If you wish to download the report, then you simply change the third parameter of ExportToHttpResponse method in Step 4 to True.

 

The following code shows how to export a report to other formats including Word and Excel.

 

' Export report to the selected format

If ExportFormatList.SelectedItem.Text = "Adobe (PDF)" Then

                report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "ExportedReport")

            ElseIf ExportFormatList.SelectedItem.Text = "MS Word" Then

                report.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, True, "ExportedReport")

            ElseIf ExportFormatList.SelectedItem.Text = "MS Excel 97 - 2000" Then

                report.ExportToHttpResponse(ExportFormatType.Excel, Response, True, "ExportedReport")

            ElseIf ExportFormatList.SelectedItem.Text = "MS Excel 97 - 2000 (Data Only)" Then

                report.ExportToHttpResponse(ExportFormatType.ExcelRecord, Response, True, "ExportedReport")

            End If

Login to add your contents and source code to this article
share this article :
post comment
 

Hi,
I have a crystal report which includes 4 subreports. When I preview the report, it works fine. But when i export it into a pdf file, it has some mistake. The report in pdf file has a section repeats in many times.
Anyone can help me?
Thanks in advance


Posted by Lee Sep 14, 2010

Hi Mahesh,

This article is really helpfull. But I am stuck with passing one of the parameters, that is Response. So could you please explain what is the Response parameter in the above ExportToHttpResponse() method call?

Regards,
Narsing

Posted by Narsingrao Chavan Jun 04, 2010

how one can save crystalreport automatically in pdf format

Posted by reshma punde May 06, 2010

it may be your OLE objects. Wrong paths.

Posted by Michael Braun Jun 06, 2009

I am not sure. You may want to post your question on forums.

Posted by Mahesh Chand Apr 07, 2009
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor