protected void Write()
{
MemoryStream m = new MemoryStream();
Document doc = new Document();
doc.SetMargins(50,20,30,30);
try
{
Response.ContentType = "application/pdf";
PdfWriter writer = PdfWriter.GetInstance(doc, m);
writer.CloseStream = false;
itsEvents ev = new itsEvents();
writer.PageEvent = ev;
ev.Header = insstr;
doc.Open();
PdfPTable tabla = gpdf.TablePDF(Adjudicatarios);
tabla.SetWidths(new Single[] {20,20,20,10,10,20,10,100,20,30});
doc.Add(tabla);
doc.Close();
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.StackTrace);
Console.Error.WriteLine(ex.Message);
}
// step 6: Write pdf bytes to outputstream
Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
m.Close();
}
public class GestionPDF
{
String titulo;
public GestionPDF()
{
//
// TODO: Agregar aquí la lógica del constructor
//
}
// add a table to the PDF document
public PdfPTable TablePDF(DataTable tabla)
{
string[] col;
col = new string[tabla.Columns.Count];
int k = 0;
foreach (DataColumn cl in tabla.Columns)
{
col[k] = cl.ColumnName;
++k;
}
PdfPTable table = new PdfPTable(tabla.Columns.Count);
table.WidthPercentage = 100;
table.SpacingBefore = 10;
for (int i = 0; i < col.Length; ++i)
{
PdfPCell cell = new PdfPCell(new Phrase(col[i]));
cell.BackgroundColor = new BaseColor(204, 204, 204);
table.AddCell(cell);
}
foreach (DataRow row in tabla.Rows)
{
foreach (DataColumn cl in tabla.Columns)
{
table.AddCell(row[cl].ToString());
}
}
return table;
}
}
public class itsEvents : PdfPageEventHelper
{
private String _Header;
public String Header
{
get { return _Header; }
set { _Header = value; }
}
public override void OnStartPage(PdfWriter writer, Document document)
{
Paragraph p = new Paragraph(Header + " " + DateTime.Now.ToString() + " " + writer.PageNumber);
p.SpacingBefore = 10;
document.Add(p);
}
}
Cita: