<div> <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> <br /> <div> <asp:Chart ID="Chart2" 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> <br /> <div> <asp:Chart ID="Chart3" 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> <br /> <div> <asp:Chart ID="Chart4" 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> <asp:Button ID="btnExportExcel" runat="server" Text="Export to Excel" OnClick="btnExportExcel_Click" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="PDF" /> protected void btnExportExcel_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=ChartExport.xls"); Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); Chart1.RenderControl(hw); string src = Regex.Match(sw.ToString(), "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value; string img = string.Format("<img src = '{0}{1}' />", Request.Url.GetLeftPart(UriPartial.Authority), src); System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table(); TableRow row = new TableRow(); row.Cells.Add(new TableCell()); row.Cells[0].Width = 200; row.Cells[0].HorizontalAlign = HorizontalAlign.Center; //row.Cells[0].Controls.Add(new Label { Text = "Fruits Distribution (India)", ForeColor = Color.Red }); table.Rows.Add(row); row = new TableRow(); row.Cells.Add(new TableCell()); row.Cells[0].Controls.Add(new Literal { Text = img }); table.Rows.Add(row); sw = new StringWriter(); hw = new HtmlTextWriter(sw); table.RenderControl(hw); Response.Write(sw.ToString()); Response.Flush(); Response.End(); } protected void Button1_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(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)