Export Chart to PDF Using ITextSharp

This article explains how to export an ASP.Net Chart Control to PDF using iTextSharp.

What is ITextSharp

iTextSharp is a free and open source assembly that helps to convert page output or HTML content in A PDF file.

You can download it from here:


Now add that DLL to the application.

Getting Started

Start Visual Studio and create a new website in ASP.Net and add these 2 DLLs to the solution.

  • Itextsharp.dll
  • itextsharp.pdfa.dll

Now drag and drop the ASP.Net Chart Control to web page.

<asp:Chart ID="Chart1" runat="server">
                <asp:Series Name="Series1" Legend="Legend1">
                    <asp:DataPoint AxisLabel="Article" YValues="90" />
                    <asp:DataPoint AxisLabel="Blogs" YValues="120" />
                    <asp:DataPoint AxisLabel="Questions" YValues="300" />
                    <asp:DataPoint AxisLabel="Videos" YValues="240" />
                    <asp:DataPoint AxisLabel="Training" YValues="100" />
                <asp:ChartArea Name="ChartArea1">
        <titles><asp:Title Name="Title1" Text="Website Stats"></asp:Title></titles></asp:Chart>
        <br />
 <asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick="btnExport_Click" />

Once we have added the Chart Control to a page, as you can see a few things have been added to the web.config automatically.


    <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />



    <validation validateIntegratedModeConfiguration="false" />


      <remove name="ChartImageHandler" />

      <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST"

        path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />




      <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        validate="false" />




        <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting"

          assembly="System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />



Now let's work on the code behind, first of all add the following namespaces:

using iTextSharp.text;

using System.IO;

using iTextSharp.text.html.simpleparser;

using iTextSharp.text.pdf;

using System.Web.UI.DataVisualization.Charting;

Write this on button click.

protected void btnExport_Click(object sender, EventArgs e)


   Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

   PdfWriter.GetInstance(Doc, Response.OutputStream);


   using (MemoryStream memoryStream = new MemoryStream())


       Chart1.SaveImage(memoryStream, ChartImageFormat.Png);

       iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(memoryStream.GetBuffer());





        Response.ContentType = "application/pdf";

        Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");






Hit F5 to see the output.


Image 1.

Click the "Export" button to export data to a PDF.


Image 2.

If you need more help then download the sample application, there are all the DLLs available and the page name is ExportChartToPDF.aspx.

Up Next
    Ebook Download
    View all
    View all