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.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.