Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am trying to export multiple gridviews into single pdf using iTextSharp. I am looping through the gridviews and then looping through the rows of the gridview. The looping is going ok. But after pdf download, only the last gridview can be seen. It seems the gridviews are overwriting each other and only last one remains. Here is my code. What am I doing wrong?

protected void btnExportToPDF_Click(object sender, EventArgs e)
        {
            GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 };
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
            

            for (int i = 0; i < gvExcel.Length; i++)
            {
                if (gvExcel[i].Visible)
                {

                    PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);

                    foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells)
                    {
                        Font font = new Font();
                        font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                        PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
                        pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                        pdfTbl.AddCell(pdfCell);
                    }


                    foreach (GridViewRow gvRow in gvExcel[i].Rows)
                    {
                        foreach (TableCell tblCell in gvRow.Cells)
                        {
                            Font font = new Font();
                            font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                            PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
                            pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                            pdfTbl.AddCell(pdfCell);
                        }
                    }

                    //Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                    pdfDoc.Open();
                    pdfDoc.Add(pdfTbl);
                }
            }

            pdfDoc.Close();

            //Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf");
            Response.Write(pdfDoc);
            Response.Flush();
            Response.End();
        }


What I have tried:

I am sure there is somewhere the gridviews are getting overlapped. But I am not able to find where.
Posted
Updated 17-Nov-17 20:06pm

1 solution

OK...previously I was using GetInstance and Open methods inside for loop. This is my modified code.

protected void btnExportToPDF_Click(object sender, EventArgs e)
       {
           GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 };
           Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
           PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
           pdfDoc.Open();


           for (int i = 0; i < gvExcel.Length; i++)
           {
               if (gvExcel[i].Visible)
               {

                   PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);

                   foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells)
                   {
                       Font font = new Font();
                       font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                       PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
                       pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                       pdfTbl.AddCell(pdfCell);
                   }


                   foreach (GridViewRow gvRow in gvExcel[i].Rows)
                   {
                       foreach (TableCell tblCell in gvRow.Cells)
                       {
                           Font font = new Font();
                           font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                           PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
                           pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                           pdfTbl.AddCell(pdfCell);
                       }
                   }
                   pdfDoc.Add(pdfTbl);

               }
           }

           pdfDoc.Close();

           //Response.Clear();
           Response.ContentType = "application/pdf";
           Response.AppendHeader("content-disposition", "attachment;filename=report" + startDate + "-" + endDate + ".pdf");
           Response.Write(pdfDoc);
           Response.Flush();
           Response.End();
       }


Though all the gridviews are coming, but they are stuck one end to other front making it look like a huge single table.
 
Share this answer
 
Comments
VAIBHAV RAWAT 7-Apr-21 8:11am    
font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor)
this line give me an error in my code

System.Drawing.Font' does not contain a definition for 'Color' and no extension method 'Color' accepting a first argument of type 'System.Drawing.Font' could be found (are you missing a using directive or an assembly reference?

and my pdf which i got is dark in background color

please help me,i am new in dot net

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