Tengo un problema al momento de usar el header de un pdf hecho con itextsharp
ya que se sobreescribe una tabla encima del el (El header es lo que dice "TITULO DEL PDF").
¿Cómo puedo hacer para que esto no ocurra?
Adjunto código e imagen:
Lo que está marcado en amarillo es el header y la tabla se pone encima de el:
protected void btnPDF_Click(object sender, EventArgs e)
{
PDF();
}
void PDF()
{
PruebaChart_BC lobGrafica = new PruebaChart_BC();
DataTable dtGrafica = lobGrafica.Grafica();
MemoryStream ms = new MemoryStream();
string filename = null;
Document document = new Document(iTextSharp.text.PageSize.LETTER, 20, 20, 20, 20);
PdfWriter pw = PdfWriter.GetInstance(document, ms);
pw.PageEvent = new HeaderFooter();
document.Open();
PdfPTable table = new PdfPTable(dtGrafica.Columns.Count);
BaseFont bff = BaseFont.CreateFont(BaseFont.HELVETICA,BaseFont.CP1250,BaseFont.EMBEDDED);
Font fontText = new Font(bff,10,0,BaseColor.BLACK);
Font Columnas = new Font(bff, 9, 1, BaseColor.BLACK);
PdfContentByte cb = pw.DirectContent;
table.WidthPercentage = 100f;
table.TotalWidth = 100f;
document.Add(new Paragraph("Luis Alberto es el mejor del mundo"));
document.Add(new Paragraph("Y dani es super gay"));
document.Add(new Paragraph(""));
filename = "PdfLuis";
foreach (DataColumn c in dtGrafica.Columns)
{
PdfPCell _cell = new PdfPCell();
//table.AddCell(new Paragraph(c.ColumnName, Columnas));
_cell = new PdfPCell(new Paragraph(c.ColumnName, Columnas));
_cell.HorizontalAlignment = Element.ALIGN_CENTER;
_cell.BackgroundColor = BaseColor.CYAN;
table.AddCell(_cell);
}
foreach (DataRow r in dtGrafica.Rows)
{
if (dtGrafica.Rows.Count > 0)
{
PdfPCell _cell = new PdfPCell();
//table.AddCell(new Paragraph(r[0].ToString(), fontText));
_cell = new PdfPCell(new Paragraph(r[0].ToString(), fontText));
_cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(_cell);
//table.AddCell(new Paragraph(r[1].ToString(), fontText));
_cell = new PdfPCell(new Paragraph(r[1].ToString(), fontText));
_cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(_cell);
//table.AddCell(new Paragraph(r[2].ToString(), fontText));
_cell = new PdfPCell(new Paragraph(r[2].ToString(), fontText));
_cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(_cell);
//table.AddCell(new Paragraph(r[3].ToString(), fontText));
_cell = new PdfPCell(new Paragraph(r[3].ToString(), fontText));
_cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(_cell);
}
}
//table.WriteSelectedRows(0, -1, 200, 550, cb);
document.Add(table);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=" + filename + ".pdf"));
Response.BinaryWrite(ms.ToArray());
}
class HeaderFooter : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
//tbHeader.WriteSelectedRows(0, -1, page.Left + document.LeftMargin, page.Top, writer.DirectContent);
//(0,-1,document.LeftMargin, writer,PageSize.GetTop(document.TopMargin) + 40,writer.DirectContent);
PdfPTable tbFooter = new PdfPTable(3);
tbFooter.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
tbFooter.DefaultCell.Border = 0;
tbFooter.AddCell(new Paragraph());
PdfPCell _cell2 = new PdfPCell(new Paragraph("PARTE DE ABAJO"));
_cell2.HorizontalAlignment = Element.ALIGN_CENTER;
_cell2.Border = 0;
tbFooter.AddCell(_cell2);
_cell2 = new PdfPCell(new Paragraph("Pagina: " + writer.PageNumber));
_cell2.HorizontalAlignment = Element.ALIGN_RIGHT;
_cell2.Border = 0;
tbFooter.AddCell(_cell2);
// tbFooter.WriteSelectedRows(0, -1, 0, (document.PageSize.Height - 50), writer.DirectContent);
//tbHeader.WriteSelectedRows(0, -1, document.LeftMargin, writer, PageSize, writer.DirectContent);
//tbHeader.WriteSelectedRows(0, -1, document.LeftMargin, document.Top, writer.DirectContent);
//tbHeader.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
tbFooter.WriteSelectedRows(0, -1, 10, tbFooter.TotalHeight, writer.DirectContent);
}
public override void OnStartPage(PdfWriter writer, Document document)
{
PdfPTable tbHeader = new PdfPTable(3);
tbHeader.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
tbHeader.DefaultCell.Border = 0;
tbHeader.AddCell(new Paragraph());
PdfPCell _cell = new PdfPCell(new Paragraph("TITULO DEL PDF"));
_cell.HorizontalAlignment = Element.ALIGN_CENTER;
_cell.Border = 0;
tbHeader.AddCell(_cell);
tbHeader.AddCell(new Paragraph());
tbHeader.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
//(0, -1, 10, tbFooter.TotalHeight, writer.DirectContent);
}
}
}