locked
Chart Image does not show in PDF using IText sharp RRS feed

  • Question

  • User82362805 posted

    gridview and chart control both export in PDF on button click in asp.net but gridview export perfectly with all the records but there is just image of chart control has rendered in pdf the points and other information did not show in chart you can see the result in attached PDF

    Employee PDF

    Here is the code for PDF

     protected void btnPrint_Click(object sender, EventArgs e)
            {
               
                int columnsCount = EmployeePerformance.HeaderRow.Cells.Count;
               
                PdfPTable pdfTable = new PdfPTable(columnsCount);
    
              
                foreach (TableCell gridViewHeaderCell in EmployeePerformance.HeaderRow.Cells)
                {
                   
                    Font font = new Font();
                   
                    font.Color = new BaseColor(EmployeePerformance.HeaderStyle.ForeColor);
    
                   
                    PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewHeaderCell.Text, font));
    
                  BackgroundColor color
                    pdfCell.BackgroundColor = new BaseColor(EmployeePerformance.HeaderStyle.BackColor);
    
                    
                    pdfTable.AddCell(pdfCell);
                }
    
               
                foreach (GridViewRow gridViewRow in EmployeePerformance.Rows)
                {
                    if (gridViewRow.RowType == DataControlRowType.DataRow)
                    {
                       
                        foreach (TableCell gridViewCell in gridViewRow.Cells)
                        {
                            Font font = new Font();
                            font.Color = new BaseColor(EmployeePerformance.RowStyle.ForeColor);
    
                            PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewCell.Text, font));
    
                            pdfCell.BackgroundColor = new BaseColor(EmployeePerformance.RowStyle.BackColor);
    
                            pdfTable.AddCell(pdfCell);
                        }
                    }
                }
    
               
                Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    
                PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
    
                pdfDocument.Open();
                using (MemoryStream stream = new MemoryStream())
                {
                    Chart1.SaveImage(stream, ChartImageFormat.Png);
                    iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
                    chartImage.ScalePercent(75f);
    
    
                    pdfDocument.Add(pdfTable);
                    pdfDocument.Add(chartImage);
                    pdfDocument.Close();
    
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("content-disposition",
                        "attachment;filename=EmployeePerformance.pdf");
                    Response.Write(pdfDocument);
                    Response.Flush();
                    Response.End();
                }
            }

    Monday, December 24, 2018 1:32 PM

Answers

  • User82362805 posted

    This problem has been solved I did not call the method on page load event where chart data has bind 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 24, 2018 2:27 PM