Unable to Export Dynamic Charts to PDF
Hi Guys,
I have an ASP chart nested inside a table and the chart is being generated based on the values available in the dropdown
I am able to export the contents in the table but the chart is not being exported.Can anyone help me out on this.
Please find the code below:
ASPX:
<asp:DropDownList ID="DropDownList1" runat="server" Height="26px" Width="225px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList >
</p>
<asp:Chart ID="Chart1" runat="server">
<Series>
<asp:Series Name="Series1" ChartType="Line" YAxisType="Primary">
</asp:Series>
<asp:Series Name="Series2" ChartType="Column" YAxisType="Secondary">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
<asp:Button ID="btnExportPDF" runat="server" Text="Export to PDF"
onclick="btnExportPDF_Click" />
C Sharp Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = SQLConnection();
SqlCommand command = new SqlCommand("Select distinct color from [AdventureWorks2008R2].[Production].[Product]", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "color";
DropDownList1.DataValueField = "color";
DropDownList1.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedvalue = DropDownList1.SelectedValue;
SqlConnection connection = SQLConnection();
SqlCommand command = new SqlCommand("SELECT [Name],[SafetyStockLevel],[ReorderPoint] FROM [AdventureWorks2008R2].[Production].[Product] where [Color] = '" + selectedvalue + "'", connection);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
Chart1.DataSource = ds;
Chart1.Series["Series1"].XValueMember = "Name";
Chart1.Series["Series1"].YValueMembers = "SafetyStockLevel";
//Chart1.Series["Series2"].XValueMember = "Name";
Chart1.Series["Series2"].YValueMembers = "ReorderPoint";
Chart1.DataBind();
connection.Close();
}
protected void btnExportPDF_Click(object sender, EventArgs e)
{
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();
}
}