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();

    }

}

 

 

cytotec abortion

an abortion pill buy abortion pills online
medical abortion pill online cytotec abortion abortion pill


View demo Download
http://www.dotnetfox.com/Document/1117/0photo5.jpg

If you enjoyed this article, get email updates (it's free).


Related Articles
  • Export GridView data to excel using ASP.NET and c# In this article I’m going to explain how to export GridView data to excel using ASP.NET and C#. Sometimes we need to export gridview data to excel. Here I’ll show you how to export GridView data to Excel by using RkLib ExportData. RKLib ExportData is a class library used to export data to excel. So here we have to create table and fetch the records to DataTable and we can export to excel.
  • How to export GridView data to PDF document in ASP.NET with C# using iTextSharpIn this article I’m going to explain how to export GridView to PDF document. Other articles related to iTextSharp. iTextSharp is a C# port of iText, and open source Java library for PDF generation and manipulation. It can be used to create PDF documents from scratch, to convert XML to PDF (using the extra XFA Worker DLL), to fill out interactive PDF forms, to stamp new content on existing PDF doc...
  • how to export data from DataTable to Excel in ASP.NET using C#. In this article I’m going to explain how to export data from DataTable to Excel in ASP.NET using C#. Sometime we need to export data from DataTable to excel. Here I’ll show you how to export data to Excel by using RkLib ExportData. RKLib ExportData is a class library used to export data to excel. So here we have to create table and fetch records to DataTable and we can export to excel.
  • Export Chart control to PDF document in ASP.NET using iTextSharp and C#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 docume...
  • Export HTML Table to PDF in ASP.NET In this article I’m going to explain how to export HTML table to PDF in ASP.NET. There are many ways we can export HTML Table to PDF document. Here I’ll explain the easiest way to export HTML Table to PDF. WebClient class provide the feature to download the HTML string from current URL. First we need to make sure that how we can extract HTML Table from entire source code and how we can get conten...

Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox