In this article I will explain with an example, how to export Crystal Report to PDF programmatically in ASP.Net with C# and VB.Net.
Exporting Crystal Report to PDF programmatically means, exporting from Code-Behind without using the CrystalReportViewer control in ASP.Net.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
HTML Markup
The HTML Markup consists of a CrystalReportViewer control, a RadioButtonList with the Export file formats and a Button to export Crystal Reports to Word, Excel, PDF and CSV file formats.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>   
    <script src='<%=ResolveUrl("~/crystalreportviewers13/js/crviewer/crv.js")%>' type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
        Height="400" Width="600" BestFitPage="False" ToolPanelView="None" />
    <br />
    Format:
    <asp:RadioButtonList ID="rbFormat" runat="server" RepeatDirection="Horizontal">
        <asp:ListItem Text="Word" Value="Word" Selected="True" />
        <asp:ListItem Text="Excel" Value="Excel" />
        <asp:ListItem Text="PDF" Value="PDF" />
        <asp:ListItem Text="CSV" Value="CSV" />
    </asp:RadioButtonList>
    <br />
    <asp:Button ID="btnExport" Text="Export" runat="server" OnClick="Export" />
    </form>
</body>
</html>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
 
 
Designing and populating the Crystal Report from Database
Inside the Page Load event, the Crystal Report is populated from database.
Note: For more details about designing and populating Crystal Report, please refer the following article.
        Crystal Report ASP.Net Example using DataSet or DataTable in C# VB.Net and Visual Studio 2010
 
C#
ReportDocument crystalReport;
protected void Page_Load(object sender, EventArgs e)
{
    crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
    Customers dsCustomers = this.GetData("SELECT TOP 5 * FROM customers");
    crystalReport.SetDataSource(dsCustomers);
    CrystalReportViewer1.ReportSource = crystalReport;
}
 
private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
 
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}
 
VB.Net
Private crystalReport As ReportDocument
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    crystalReport = New ReportDocument()
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
    Dim dsCustomers As Customers = Me.GetData("SELECT TOP 5 * FROM customers")
    crystalReport.SetDataSource(dsCustomers)
    CrystalReportViewer1.ReportSource = crystalReport
End Sub
 
Private Function GetData(query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsCustomers As New Customers()
                sda.Fill(dsCustomers, "DataTable1")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function
 
 
Exporting Crystal Report to PDF programmatically in ASP.Net
When the Export Button is clicked, based on the selection from the RadioButtonList the ExportFormatType for the Crystal Report is set.
Then based on the ExportFormatType the Crystal Report is exported to the desired format using the ExportToHttpResponse method.
The Crystal Report exported document i.e. Word, Excel, PDF or CSV will be downloaded as Attachment in Browser.
C#
protected void Export(object sender, EventArgs e)
{
    ExportFormatType formatType = ExportFormatType.NoFormat;
    switch (rbFormat.SelectedItem.Value)
    {
        case "Word":
            formatType = ExportFormatType.WordForWindows;
            break;
        case "PDF":
            formatType = ExportFormatType.PortableDocFormat;
            break;
        case "Excel":
            formatType = ExportFormatType.Excel;
            break;
        case "CSV":
            formatType = ExportFormatType.CharacterSeparatedValues;
            break;
    }
 
    crystalReport.ExportToHttpResponse(formatType, Response, true, "Crystal");
    Response.End();
}
 
VB.Net
Protected Sub Export(sender As Object, e As EventArgs)
    Dim formatType As ExportFormatType = ExportFormatType.NoFormat
    Select Case rbFormat.SelectedItem.Value
        Case "Word"
            formatType = ExportFormatType.WordForWindows
            Exit Select
        Case "PDF"
            formatType = ExportFormatType.PortableDocFormat
            Exit Select
        Case "Excel"
            formatType = ExportFormatType.Excel
            Exit Select
        Case "CSV"
            formatType = ExportFormatType.CharacterSeparatedValues
            Exit Select
    End Select
    crystalReport.ExportToHttpResponse(formatType, Response, True, "Crystal")
    Response.End()
End Sub
 
 
Screenshots
The Crystal Report
Export Crystal Report to PDF programmatically in ASP.Net
 
Crystal Report exported to PDF file
Export Crystal Report to PDF programmatically in ASP.Net
 
 
Demo
 
 
Downloads