Generate PDF through Crystal Report in ASP.Net (C#)

Here is how we can generate the PDF using Crystal Report. You just need to ensure whether you have installed Crystal Report or not.
Here is the .aspx and .cs page below and my PDF is getting stored in Reports folder.
.aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PdfGenerator.aspx.cs" Inherits=" PdfGenerator" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=642fbea5521e1007"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Generate PDF through Crystal Report</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnConvertReport" runat="server" Text="Convert Report" OnClick="convertReport" />
   
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" EnableParameterPrompt="true" />

        <asp:Label ID="lblMsg" runat="server" Visible="false"></asp:Label>
 </div>
    </form>
  
</body>
</html>

.aspx.cs page:
   protected void convertReport(object sender, EventArgs e)
    {
        try
        {
            cryRptDoc = new ReportDocument();
            cryRptDoc.Load(Server.MapPath("~//Reports//myReport.rpt"));
            CrystalReportViewer1.ReportSource = cryRptDoc;
            CrystalReportViewer1.RefreshReport();


            ExportOptions CrystalReportExportOptions;
            DiskFileDestinationOptions CrystalDiskFileDestinationOptions = new DiskFileDestinationOptions();
            PdfRtfWordFormatOptions CrystalFormatTypeOptions = new PdfRtfWordFormatOptions();
            CrystalDiskFileDestinationOptions.DiskFileName = Server.MapPath("~/Reports/myReport.pdf");
            CrystalReportExportOptions = cryRptDoc.ExportOptions;
            {
                CrystalReportExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrystalReportExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                CrystalReportExportOptions.DestinationOptions = CrystalDiskFileDestinationOptions;
                CrystalReportExportOptions.FormatOptions = CrystalFormatTypeOptions;
            }
            cryRptDoc.Export();
        }
        catch (Exception ex)
        {
            lblMsg.Visible = true;
            lblMsg.Text = "There is some problem while generating the PDF file";
        }
    }

I hope it helps you out.
Cheers,

ved pathak

Comments