protected void Button5_Click(object sender, EventArgs e) { if (GVbebidas.HeaderRow == null && GVplatos.HeaderRow == null) { } else { negome.LiberarMesa(Convert.ToInt64(ddlmesa.SelectedItem.Value), false); negocio.CobrarPedidos(Convert.ToInt64(ddlmesa.SelectedItem.Value)); int columnsCount = 0; if (GVplatos.HeaderRow == null) { columnsCount = GVbebidas.HeaderRow.Cells.Count; } else if (GVbebidas.HeaderRow == null) { columnsCount = GVplatos.HeaderRow.Cells.Count; } else { columnsCount = GVbebidas.HeaderRow.Cells.Count + GVplatos.HeaderRow.Cells.Count; } // Create the PDF Table specifying the number of columns PdfPTable pdfTable = new PdfPTable(columnsCount); pdfTable.AddCell(" Total= "); pdfTable.AddCell("$" + total); pdfTable.AddCell(" "); pdfTable.AddCell(" "); pdfTable.AddCell(" "); pdfTable.AddCell(" "); Session["cantidadColumnas"] = columnsCount; if (GVplatos.HeaderRow != null) { // Loop thru each cell in GrdiView header row foreach (TableCell gridViewHeaderCell in GVplatos.HeaderRow.Cells) { // Create the Font Object for PDF document Font font = new Font(); // Set the font color to GridView header row font color font.Color = new BaseColor(GVplatos.HeaderStyle.ForeColor); // Create the PDF cell, specifying the text and font PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewHeaderCell.Text, font)); // Set the PDF cell backgroundcolor to GridView header row BackgroundColor color pdfCell.BackgroundColor = new BaseColor(GVplatos.HeaderStyle.BackColor); // Add the cell to PDF table pdfTable.AddCell(pdfCell); } // Loop thru each datarow in GrdiView foreach (GridViewRow gridViewRow in GVplatos.Rows) { if (gridViewRow.RowType == DataControlRowType.DataRow) { // Loop thru each cell in GrdiView data row foreach (TableCell gridViewCell in gridViewRow.Cells) { Font font = new Font(); font.Color = new BaseColor(GVplatos.RowStyle.ForeColor); PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewCell.Text, font)); pdfCell.BackgroundColor = new BaseColor(GVplatos.RowStyle.BackColor); pdfTable.AddCell(pdfCell); } } } } if (GVbebidas.HeaderRow != null) { foreach (TableCell gridViewHeaderCell in GVbebidas.HeaderRow.Cells) { // Create the Font Object for PDF document Font font = new Font(); // Set the font color to GridView header row font color font.Color = new BaseColor(GVbebidas.HeaderStyle.ForeColor); // Create the PDF cell, specifying the text and font PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewHeaderCell.Text, font)); // Set the PDF cell backgroundcolor to GridView header row BackgroundColor color pdfCell.BackgroundColor = new BaseColor(GVbebidas.HeaderStyle.BackColor); // Add the cell to PDF table pdfTable.AddCell(pdfCell); } // Loop thru each datarow in GrdiView foreach (GridViewRow gridViewRow in GVbebidas.Rows) { if (gridViewRow.RowType == DataControlRowType.DataRow) { // Loop thru each cell in GrdiView data row foreach (TableCell gridViewCell in gridViewRow.Cells) { Font font = new Font(); font.Color = new BaseColor(GVbebidas.RowStyle.ForeColor); PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewCell.Text, font)); pdfCell.BackgroundColor = new BaseColor(GVbebidas.RowStyle.BackColor); pdfTable.AddCell(pdfCell); } } } } pdfTable.AddCell("Total= $" + total); // Create the PDF document specifying page size and margins Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f); // Roate page using Rotate() function, if you want in Landscape // pdfDocument.SetPageSize(PageSize.A4.Rotate()); // Using PageSize.A4_LANDSCAPE may not work as expected // Document pdfDocument = new Document(PageSize.A4_LANDSCAPE, 10f, 10f, 10f, 10f); PdfWriter.GetInstance(pdfDocument, Response.OutputStream); LabelExito.Text = "COBRADO CON EXITO, se te descargo en PDF el detalle "; PlatosCobrar(); BebidasCobrar(); pdfDocument.Open(); pdfDocument.Add(pdfTable); pdfDocument.Close(); Response.ContentType = "application/pdf"; Response.AppendHeader("content-disposition", "attachment;filename=Employees.pdf"); Response.Write(pdfTable); Response.Flush(); Response.End(); } }