Thursday, February 9, 2012

Export Chart to Pdf in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.DataVisualization.Charting;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Color = System.Drawing.Color;
using Font = System.Drawing.Font;

public partial class Chart2Pdf : System.Web.UI.Page
{
Chart chart = new Chart();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["dbstring"].ToString());

protected void Page_Load(object sender, EventArgs e)
{

}

public void LoadchartControl()
{
DataSet ds = new DataSet();
SqlDataAdapter da=new SqlDataAdapter("select * from TestEmployee",con);
da.Fill(ds);

this.chart.Width = 500;
this.chart.Height = 500;

this.chart.Legends.Add("Legend1");
this.chart.Legends[0].Enabled = true;
this.chart.Legends[0].Docking = Docking.Bottom;
this.chart.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
chart.Titles.Add("Sales By Employee");
chart.Titles[0].Font = new Font("Arial", 16f);

chart.ChartAreas.Add("");
chart.ChartAreas[0].AxisX.Title = "Employee";
chart.ChartAreas[0].AxisY.Title = "Sales";
chart.ChartAreas[0].AxisY.Interval = 100;
chart.ChartAreas[0].AxisX.TitleFont = new Font("Arial", 12f);
chart.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 12f);
chart.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Arial", 10f);
chart.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
chart.ChartAreas[0].BackColor = Color.White;
chart.ChartAreas[0].Area3DStyle.Enable3D = true;
chart.ChartAreas[0].Area3DStyle.Inclination = 50;

chart.Series.Add("");
chart.Series[0].ChartType = SeriesChartType.Column;
this.chart.Series[0].BorderWidth = 1;
this.chart.Series[0].BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
this.chart.Series[0].XValueMember = "Employee";
this.chart.Series[0].YValueMembers = "Sales";
chart.DataSource = ds;
chart.DataBind();

}

void ExportToPdf()
{
//Load the chart control as you normally do whenever you load it.

LoadchartControl();

chart.SaveImage(Server.MapPath( "~/chartImages/chart.png"), ChartImageFormat.Png);
Document document = new Document(PageSize.LETTER, 72, 72, 82, 72);
MemoryStream msReport = new MemoryStream();

try
{

PdfWriter writer = PdfWriter.GetInstance(document, msReport);
document.AddAuthor("Test");
document.AddSubject("Export to PDF");
document.Open();
Chunk c = new Chunk("Export chart to PDF", FontFactory.GetFont("VERDANA", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
iTextSharp.text.Image hImage;
hImage = iTextSharp.text.Image.GetInstance(MapPath("~/chartImages/chart.png"));

float NewWidth = 500;
float MaxHeight = 400;

if (hImage.Width <= NewWidth) { NewWidth = hImage.Width; } float NewHeight = hImage.Height * NewWidth / hImage.Width; if (NewHeight > MaxHeight)
{
NewWidth = hImage.Width * MaxHeight / hImage.Height;
NewHeight = MaxHeight;
}

float ratio = hImage.Width / hImage.Height;
hImage.ScaleAbsolute(NewWidth, NewHeight);
document.Add(p);
document.Add(hImage);
document.Close();

Response.AddHeader("Content-type", "application/pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=chart.pdf");
Response.OutputStream.Write(msReport.GetBuffer(), 0, msReport.GetBuffer().Length);

}
catch (System.Threading.ThreadAbortException ex)
{
throw new Exception("Error occured: " + ex);
}

}

protected void btnExport_Click(object sender, EventArgs e)
{
ExportToPdf();
}

}

No comments:

Post a Comment