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

Default.aspx
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<div>
<br />
<asp:Button ID="btnGerarPDF" runat="server" OnClick="btnGerarPDF_Click" Text="PDF" />
</div>
Default.aspx.cs
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
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)
{
// transforma o List para um DataTable para facilitar o exemplo
DataTable table = ConverteListParaDataTable<Pessoa>(Pessoa.Get());
GeneratePDF(table);
}
// recebe um DataTable como parâmetro
private void GeneratePDF(DataTable dataTable)
{
// cria o PDF
Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
// trabalha em memória
MemoryStream mStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
// numero de colunas e linhas
int cols = dataTable.Columns.Count;
int rows = dataTable.Rows.Count;
pdfDoc.Open();
// cria tabela
PdfPTable pdfTable = new PdfPTable(cols);
// cabeçalhos
for (int i = 0; i < cols; i++)
{
PdfPCell cellCols = new PdfPCell();
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
Chunk chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);
cellCols.Phrase = new Phrase(chunkCols);
pdfTable.AddCell(cellCols);
}
// dados
for (int k = 0; k < rows; k++)
for (int j = 0; j < cols; j++)
{
PdfPCell cellRows = new PdfPCell();
Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
cellRows.Phrase = new Phrase(chunkRows);
pdfTable.AddCell(cellRows);
}
pdfDoc.Add(pdfTable);
pdfDoc.Close();
// joga pro navegador
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
Response.Clear();
Response.BinaryWrite(mStream.ToArray());
Response.End();
}
// função auxilixar - não obrigatória
public DataTable ConverteListParaDataTable<T>(List<T> list)
{
DataTable dt = new DataTable();
foreach (PropertyInfo info in typeof(T).GetProperties())
{
dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
foreach (T t in list)
{
DataRow row = dt.NewRow();
foreach (PropertyInfo info in typeof(T).GetProperties())
{
row[info.Name] = info.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
}
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;
}
}