Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi, i am facing this error when i tried to export a chart that i created. The error shows that "could not download chart image". What should i do? I am not creating a dynamic chart, it is bind with datareader..can any1 suggest a way to export chart at e most easy way?

code:
C#
protected void ExportToPdf_Click(object sender, EventArgs e)
        {
            Exportchart(this.Chart1);
        }
protected void Exportchart(Chart chart)
        {
            chart.SaveImage(Server.MapPath("./Image/chart.png"));
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 72, 72, 82, 72);
            MemoryStream msReport = new MemoryStream();

            try
            {
                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, msReport);
                document.AddAuthor("Test");
                document.AddSubject("Export to PDF");
                document.Open();
                iTextSharp.text.Chunk c = new iTextSharp.text.Chunk("Export chart to PDF", iTextSharp.text.FontFactory.GetFont("VERDANA", 15));
                iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();
                p.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                iTextSharp.text.Image hImage;
                hImage = iTextSharp.text.Image.GetInstance(Server.MapPath("./Image/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);
            }
        }
Posted
Updated 25-Sep-12 8:24am
v3

 
Share this answer
 
Well you can have a look on a CP article for reference: Export .NET MSChart to Excel/PDF Using Report Viewer 2010[^]
 
Share this answer
 
Comments
Maciej Los 25-Sep-12 16:18pm    
Excellent, +5!
 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();
 
Share this answer
 
Comments
Member 9364222 17-Oct-12 3:09am    
Hi i am using export function to export the charts to pdf. it works fine and good.I have options to change the type of the chart at runtime using dropdownlist.when the chart type is changed and when i try to export it the default type while pageload is exported. i Want to export the changed chart type .Is there any solution for this.
pepcoder 4-Apr-13 3:12am    
Hi,
The code mentioned in "Solution 2" exports graph details in to .pdf file. But at the same it gives an exception also "Error occurred System Threading ThreadAbortException Thread was being aborted". Can anybody please help to fix this issue. Also please let me know how can I export multiple graphs from the same page into a pdf file.
pepcoder 4-Apr-13 3:49am    
Hello,
I have fixed the issue of the exception which was throwing while exporting as .pdf file. Can someone please suggest me how can I export multiple graphs into .pdf files.
thanks
pep

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900