In
this article I’m going to explain how to export ASP.NET chart control to PDF using
C#
I have already written an article about bind ASP.NET
Chart control from database. You could refer this article by following this
below link
Bind ASP.NET chart
control from database using C#
If you are not aware to include iTextSharp.dll
in ASP.NET project you could read this article
How to create PDF
document in ASP.NET with C# using iTextSharp
Here I’ll show you how to export ASP.NET chart control to PDF using iTextSharp and C#.
The Chart controls enable you to create
ASP.NET pages with simple, intuitive, and visually compelling charts for
complex statistical or financial analysis. If it’s required we have to export
Chart report to PDF document.
Designer
source code:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<%@ Register Assembly="System.Web.DataVisualization,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting"
TagPrefix="asp"
%>
<!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>ASP.NET chart</title>
</head>
<body>
<form id="form1" runat="server">
<div style="padding-left:200px">
<asp:Chart ID="Chart1" runat="server"
Height="300px"
Width="400px"
>
<Titles>
<asp:Title ShadowOffset="3"
Name="Items"
/>
</Titles>
<Legends>
<asp:Legend Alignment="Center"
Docking="Bottom"
IsTextAutoFit="False"
Name="Default"
LegendStyle="Row"
/>
</Legends>
<Series>
<asp:Series Name="Default"
/>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"
BorderWidth="0"
/>
</ChartAreas>
</asp:Chart>
</div>
<div>
<asp:Button ID="btnPrint"
runat="server"
Text="Print"
OnClick="btnPrint_Click"/>
</div>
</form>
</body>
</html>
C#
code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.DataVisualization.Charting;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
public partial class
_Default : System.Web.UI.Page
{
//get connection string from web.config
SqlConnection conn = new
SqlConnection("Data
Source=SPIDER;Initial Catalog=Northwind;Integrated Security=True");
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindChart();
}
}
protected void
BindChart()
{
DataSet ds = new DataSet();
DataTable dt = new
DataTable();
conn.Open();
string cmdstr = "select
top 6 Country, COUNT(CompanyName) [Total Suppliers] from [Suppliers] group by
Country";
SqlCommand cmd = new
SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new
SqlDataAdapter(cmd);
adp.Fill(ds);
dt =
ds.Tables[0];
string[] x = new string[dt.Rows.Count];
int[] y = new int[dt.Rows.Count];
for (int i = 0; i
< dt.Rows.Count; i++)
{
x[i]
= dt.Rows[i][0].ToString();
y[i]
= Convert.ToInt32(dt.Rows[i][1]);
}
Chart1.Series[0].Points.DataBindXY(x,y);
Chart1.Series[0].ChartType = SeriesChartType.Pie;
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D
= false;
Chart1.Legends[0].Enabled = true;
}
protected void
btnPrint_Click(object sener, EventArgs e)
{
BindChart();
Document pdfDoc = new
Document(PageSize.A4,
10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc,
Response.OutputStream);
pdfDoc.Open();
using (MemoryStream
stream = new MemoryStream())
{
Chart1.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage =
iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(75f);
pdfDoc.Add(chartImage);
}
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;" +
"filename=Chart.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}