Telerik Print Report Export to PDF

First create a telerik report or follow my previous post 
http://himanshu214.blogspot.in/2013/01/telerik-create-report.html 
For Print that report or Export to PDF

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%@ Register Assembly="Telerik.Web.UI, Version=2012.3.1205.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4"
    Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Assembly="Telerik.ReportViewer.WebForms, Version=6.2.12.1017, Culture=neutral, PublicKeyToken=a9d7983dfcc261be"
    Namespace="Telerik.ReportViewer.WebForms" TagPrefix="telerik" %>
<!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">
<body>
    <form id="form1" runat="server">
       <div>

       <p>Click Button For Print PDF.</p>
        <asp:Button ID="btnExport" runat="server" Text="Export To PDF"
            onclick="btnExport_Click" />
    </div>
    </form>
</body>
</html>


Default.aspx.cs:
using System;
using System.Data;
using System.Web;
using ClassLibrary1;
using Telerik.Reporting.Processing;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        Report1 r = new Report1();
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void FillData()
        {
            DataSet1 ds = new DataSet1();
            DataTable dt1 = ds.DataTable1;

            dt1.Rows.Add("Data1", "Data2", "Data3");          
            r.DataSource = ds;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            FillData();
            Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
            ReportProcessor reportProcessor = new ReportProcessor();
            instanceReportSource.ReportDocument = r;
            RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);

            string fileName = "MyReport" + "." + result.Extension;

            Response.Clear();
            Response.ContentType = result.MimeType;
            Response.Cache.SetCacheability(HttpCacheability.Private);
            Response.Expires = -1;
            Response.Buffer = true;

            Response.AddHeader("Content-Disposition", string.Format("{0};FileName=\"{1}\"", "attachment", fileName));

            Response.BinaryWrite(result.DocumentBytes);
            Response.End();
        }
    }
}

Comments

Popular posts from this blog

C# Copy files from one server to another

Export SharePoint Online Group Users to Excel using Powershell