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:

http://sourceforge.net/projects/itextsharp/ 

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.

  1. <div>  
  2.         <asp:Chart ID="Chart1" runat="server">  
  3.             <series>  
  4.                 <asp:Series Name="Series1" Legend="Legend1">  
  5.                 <Points>  
  6.                     <asp:DataPoint AxisLabel="Article" YValues="90" />  
  7.                     <asp:DataPoint AxisLabel="Blogs" YValues="120" />  
  8.                     <asp:DataPoint AxisLabel="Questions" YValues="300" />  
  9.                     <asp:DataPoint AxisLabel="Videos" YValues="240" />  
  10.                     <asp:DataPoint AxisLabel="Training" YValues="100" />  
  11.                 </Points>  
  12.                 </asp:Series>  
  13.             </series>  
  14.             <chartareas>  
  15.                 <asp:ChartArea Name="ChartArea1">  
  16.                 </asp:ChartArea>  
  17.             </chartareas>  
  18.         <titles><asp:Title Name="Title1" Text="Website Stats"></asp:Title></titles></asp:Chart>  
  19.         <br />  
  20.  <asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick="btnExport_Click" />  
  21. </div> 

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.

  1. <appSettings>  
  2.     <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />  
  3.   </appSettings>  
  4.   <system.webServer>  
  5.     <validation validateIntegratedModeConfiguration="false" />  
  6.     <handlers>  
  7.       <remove name="ChartImageHandler" />  
  8.       <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST"  
  9.         path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />  
  10.     </handlers>  
  11.   </system.webServer>  
  12.   <httpHandlers>  
  13.       <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  
  14.         validate="false" />  
  15.     </httpHandlers>  
  16.     <pages>  
  17.       <controls>  
  18.         <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting"  
  19.           assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />  
  20.       </controls>  
  21.     </pages>
Now let's work on the code behind, first of all add the following namespaces:
  1. using iTextSharp.text;  
  2. using System.IO;  
  3. using iTextSharp.text.html.simpleparser;  
  4. using iTextSharp.text.pdf;  
  5. using System.Web.UI.DataVisualization.Charting;  
  6. Write this on button click.  
  7. protected void btnExport_Click(object sender, EventArgs e)  
  8. {  
  9.    Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);  
  10.    PdfWriter.GetInstance(Doc, Response.OutputStream);  
  11.    Doc.Open();  
  12.    using (MemoryStream memoryStream = new MemoryStream())  
  13.    {  
  14.        Chart1.SaveImage(memoryStream, ChartImageFormat.Png);  
  15.        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(memoryStream.GetBuffer());  
  16.         img.ScalePercent(75f);  
  17.         Doc.Add(img);  
  18.         Doc.Close();  
  19.         Response.ContentType = "application/pdf";  
  20.         Response.AddHeader("content-disposition""attachment;filename=Chart.pdf");  
  21.         Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  22.         Response.Write(Doc);  
  23.         Response.End();  
  24.     }  
  25. }
Hit F5 to see the output.

Image1.jpg

Image 1.

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

Image2.jpg

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.