|
<< Click to Display Table of Contents >> PDF - tabela a partir de GridView |
![]() ![]()
|
PDF saída

Default.aspx
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<div>
<br />
<asp:Button ID="btnGerarPDF" runat="server" OnClick="btnGerarPDF_Click" Text="PDF" />
</div>
</form>
Default.aspx.cs
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = Pessoa.Get();
GridView1.DataBind();
}
}
protected void btnGerarPDF_Click(object sender, EventArgs e)
{
// cria o documento
Document documento = new Document(PageSize.A4, 5, 5, 15, 15);
// cria o "escritor"
PdfWriter.GetInstance(documento, new FileStream(HttpContext.Current.Server.MapPath("~/documento.pdf"), FileMode.Create));
documento.Open();
// cria tablela de 4 coluna
PdfPTable tabela = new PdfPTable(4);
// cria uma célula - será usada para cada célula abaixo
PdfPCell celula = new PdfPCell();
// monta o cabeçalho
celula.Phrase = new Phrase("Id");
tabela.AddCell(celula);
celula.Phrase = new Phrase("Nome");
tabela.AddCell(celula);
celula.Phrase = new Phrase("Valor");
tabela.AddCell(celula);
celula.Phrase = new Phrase("Data");
tabela.AddCell(celula);
Pessoa pessoa = new Pessoa();
// percorre as linhas da gridview
foreach (GridViewRow row in GridView1.Rows)
{
// converte para pessoa (obs: não dá pra usar (Pessoa)row.DataItem - apenas em eventos da Gridview)
pessoa.Id = int.Parse(row.Cells[0].Text);
pessoa.Nome = row.Cells[1].Text;
pessoa.Valor = decimal.Parse(row.Cells[2].Text);
pessoa.Data = DateTime.Parse(row.Cells[3].Text);
// manda para células
celula.Phrase = new Phrase(pessoa.Id.ToString());
celula.Phrase.Font.Color = BaseColor.RED;
celula.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
tabela.AddCell(celula);
// alinhamento normal
celula.Phrase = new Phrase(pessoa.Nome);
celula.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
tabela.AddCell(celula);
// valor alinhado a direita
celula.Phrase = new Phrase(pessoa.Valor.ToString("0.00"));
celula.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
tabela.AddCell(celula);
// data alinhada centro
celula.Phrase = new Phrase(pessoa.Data.ToString("dd/MM/yyyy"));
celula.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
tabela.AddCell(celula);
}
// coloca tabela no PDF
documento.Add(tabela);
documento.Close();
// faz download
MostrarPdf("~/documento.pdf");
}
private void MostrarPdf(string strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
Response.TransmitFile(strS);
Response.End();
Response.Flush();
Response.Clear();
}
}
Pessoa.cs
using System.Collections.Generic;
using System;
public class Pessoa
{
public int Id { get; set; }
public string Nome { get; set; }
public decimal Valor { get; set; }
public DateTime Data { get; set; }
public static List<Pessoa> Get()
{
List<Pessoa> lista = new List<Pessoa>();
lista.Add(new Pessoa() { Id = 1, Nome = "Junior", Valor = (decimal)10.02, Data = Convert.ToDateTime("01/01/2010") });
lista.Add(new Pessoa() { Id = 2, Nome = "Flavio", Valor = (decimal)98.10, Data = Convert.ToDateTime("01/01/2009") });
lista.Add(new Pessoa() { Id = 3, Nome = "Mario", Valor = (decimal)43.45, Data = Convert.ToDateTime("01/01/1990") });
lista.Add(new Pessoa() { Id = 4, Nome = "Luidi", Valor = (decimal)99.25, Data = Convert.ToDateTime("01/01/1992") });
lista.Add(new Pessoa() { Id = 5, Nome = "Peach", Valor = (decimal)76.44, Data = Convert.ToDateTime("01/01/1977") });
lista.Add(new Pessoa() { Id = 6, Nome = "Toad", Valor = (decimal)98.30, Data = Convert.ToDateTime("01/01/1980") });
lista.Add(new Pessoa() { Id = 7, Nome = "Rosalina", Valor = (decimal)25.12, Data = Convert.ToDateTime("01/01/2003") });
lista.Add(new Pessoa() { Id = 8, Nome = "Inara", Valor = (decimal)78.99, Data = Convert.ToDateTime("01/01/2007") });
return lista;
}
}