In this article I will explain with an example, how to export Chart Control to PDF using iTextSharp library in ASP.Net using C# and VB.Net.
Note: For more details on how to populate ASP.Net Charts, please refer my article Populate ASP.Net 4.0 Chart Control Pie Chart from SQL Server Database.
 
 

Download iTextSharp Library

You can download the iTextSharp library from the following link.
Note: You will need to add the reference of iTextSharp library in your project.
 
 

HTML Markup

The HTML Markup consists of following controls:
Chart – For graphical representation of data.
The Chart consists of following controls.
Titles – Contains the title configuration for the Chart.
Legends – Defines the legend settings for the Chart.
Series – Defines the series that will be plotted on the Chart.
ChartAreas – Defines the Chart area settings where the actual plotting occurs.
 
Button – For exporting Chart to PDF.
The Button has been assigned with an OnClick event handler.
<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="btnPDF" runat="server" Text="Export to PDF" OnClick="ExportToPDF" /> 
 
 

Namespaces

You will need to import the following 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

Inside the Page_Load event handler, the String and Integer Array are defined with the data.
Then, the ChartType is defined i.e. Pie and Enable3D property of Chart is set to TRUE.
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 ObjectByVal e As EventArgs) Handles Me.Load
    Dim x As String() = New String(3) { "Mango", "Apple", "Orange", "Banana" }
    Dim y As Integer() = New Integer(3) { 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 using C# and VB.Net

When the ExportToPDF Button is clicked, the Document class object is created and necessary parameter are passed.
Then, GetInstance method of PdfWriter class is called where the Document class object is passed as parameter.
The MemoryStream class object is created and Chart is saved in PNG format and Image class object is created to which the Chart Image is added.
After that, the PDF document is opened Image class object is added to it using Add method.
 
Then, the Response class properties are set.
1. ContentType – It informs the Browser about the file type. In this case it is PDF file.
2. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Note: For more details on Content Disposition Header, please refer my article What is Content Disposition Header in ASP.Net.
 
Finally, Document object is written to the Response which initiates the File download operation.
C#
protected void ExportToPDF(object sender, EventArgs e)
{
    using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f))
    {
        using (PdfWriter.GetInstance(pdfDoc, Response.OutputStream))
        {
            using (MemoryStream stream = new MemoryStream())
            {
                pdfDoc.Open();
                Chart1.SaveImage(stream, ChartImageFormat.Png);
                Image chartImage 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 ExportToPDF(ByVal sender As ObjectByVal e As EventArgs)
    Using pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
        Using PdfWriter.GetInstance(pdfDoc,Response.OutputStream)
            Using stream As MemoryStream = New MemoryStream()
                pdfDoc.Open()
                Chart1.SaveImage(stream, ChartImageFormat.Png)
                Dim chartImage As Image Image.GetInstance(stream.GetBuffer())
                chartImage.ScalePercent(75.0F)
                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 Using
        End Using
    End Using
End Sub
 
 

Screenshots

Form

Export ASP.Net Chart Control to PDF Document using iTextSharp Library
 

Exported PDF

Export ASP.Net Chart Control to PDF Document using iTextSharp Library
 
 

Browser Compatibility

The above code has been tested in the following browsers.
Microsoft Edge   FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 

Demo

 
 

Downloads