In this article I will explain how to export ASP.Net Chart Control to PDF (Portable Document File) using iTextSharp Library.
First let’s populate the Chart. Here I’ll populate a simple Pie Chart which I’ll later export to PDF document.
Note: If you want to know more about the populating ASP.Net Charts refer my following article
HTML Markup
In the below HTML Markup I have an ASP.Net Chart control and an ASP.Net button which will export the Chart to PDF.
<form id="form1" runat="server">
<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>
<br />
<asp:Button ID="btnExportPDF" runat="server" Text="Export to PDF"
onclick="btnExportPDF_Click" />
</form>
Namespaces
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Web.UI.DataVisualization.Charting
Populating the Chart
C#
protected void Page_Load(object sender, EventArgs e)
{
string[] x = new string[4] { "Mango", "Apple", "Orange", "Banana" };
int[] y = new int[4] { 200, 112, 55, 96 };
Chart1.Series[0].Points.DataBindXY(x, y);
Chart1.Series[0].ChartType = SeriesChartType.Pie;
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
Chart1.Legends[0].Enabled = true;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim x() As String = New String() {"Mango", "Apple", "Orange", "Banana"}
Dim y() As Integer = New Integer() {200, 112, 55, 96}
Chart1.Series(0).Points.DataBindXY(x, y)
Chart1.Series(0).ChartType = SeriesChartType.Pie
Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
Chart1.Legends(0).Enabled = True
End Sub
Exporting the ASP.Net Chart to PDF
On the click of the Export Button the following event handler is executed which exports the ASP.Net Chart to PDF.
C#
protected void btnExportPDF_Click(object sender, EventArgs e)
{
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();
}
}
VB.Net
Protected Sub btnExportPDF_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0!, 10.0!, 10.0!, 0.0!)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
Dim stream As MemoryStream = New MemoryStream
Chart1.SaveImage(stream, ChartImageFormat.Png)
Dim chartImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(stream.GetBuffer)
chartImage.ScalePercent(75.0!)
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()
End Sub
Demo
Downloads