Export to pdf ( Create pdf on fly ) using ASP.net and C#

Consider following code for creating pdf document on fly using ASP.net in C#.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;



namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            MemoryStream oStream;
            Response.Clear();
            Response.Buffer = true;
            ReportDocument rd = new ReportDocument();

            rd.Load(Server.MapPath("cr1.rpt"));

            oStream = (MemoryStream)rd.ExportToStream(ExportFormatType.PortableDocFormat);
            Response.ContentType = "application/pdf";

            Response.BinaryWrite(oStream.ToArray());
            Response.End();
            oStream.Flush();
            oStream.Close();
            oStream.Dispose();
        }
    }
}

Another way to Open report as a PDF Format

At first need to add following namespace

using CrystalDecisions.CrystalReports.Engine;

Then need to use following code to open it on pdf format

using (ReportClass rptH = new ReportClass())
   {
    rptH.FileName = Server.MapPath("~/")+ @"Views/Report/crJournal.rpt"; //Your rpt file path
    rptH.Load();
    rptH.SetDataSource(GetStudents());// Define/Set data Source for report
    rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
   }