I have a problem is that although print the document body, does not show the header and footer
this my controller:
public FileStreamResult Report()
{
try
{
List<dynamic> registros = HttpContext.Application["registros"] as List<dynamic>;
Dictionary<string, dynamic> primerRegistro = registros[0].Properties;
int columns = primerRegistro.Count;
MemoryStream workStream = new MemoryStream();
Document document = new Document(PageSize.LETTER, 20f, 20f, 20f, 20f);
PdfWriter writer = PdfWriter.GetInstance(document, workStream);
writer.PageEvent = new PageEventHelper();
writer.CloseStream = false;
PdfDestination pdfDest = new PdfDestination(PdfDestination.FIT, 0, document.PageSize.Height, 0);
PdfWriter.GetInstance(document, workStream).CloseStream = false;
Font helvetica = new Font(Font.FontFamily.HELVETICA, 9f);
document.Open();
PdfPTable table = new PdfPTable(columns);
table.WidthPercentage = 100;
float[] widths = new float[] { 45f, 130f, 100f, 70f, 70f, 40f };
table.SetWidths(widths);
Font dataHeaderFont = FontFactory.GetFont("Helvetica", 9, Font.BOLD, BaseColor.WHITE);
foreach (var item in primerRegistro)
{
PdfPCell cell = new PdfPCell(new Phrase(item.Key.Replace("_", " "), dataHeaderFont));
cell.BackgroundColor = BaseColor.BLACK;
table.AddCell(cell);
}
Font dataCellFont = FontFactory.GetFont("Helvetica", 9, Font.NORMAL, BaseColor.BLACK);
foreach (var registro in registros)
{
foreach (var item in registro.Properties)
{
string cellValue = item.Value.ToString();
PdfPCell cell = new PdfPCell(new Phrase(cellValue.InternalTranslateUpperFirst(), helvetica));
table.AddCell(cell);
}
}
document.Add(table);
//PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
//writer.SetOpenAction(action);
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
HttpContext.Response.AddHeader("content-disposition", "inline; filename=Reporte.pdf");
return File(workStream, "application/pdf");
}
catch (Exception ex)
{
string exmsj = ex.Message;
return null;
}
}
and this my overrides:
public class PageEventHelper : PdfPageEventHelper
{
public override void OnOpenDocument(PdfWriter writer, Document document)
{
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
tabFot.SpacingAfter = 10F;
PdfPCell cell;
tabFot.TotalWidth = 300F;
cell = new PdfPCell(new Phrase("Header"));
tabFot.AddCell(cell);
PdfContentByte canvas = writer.DirectContent;
if (canvas.InternalBuffer.Length > 0)
{
tabFot.WriteSelectedRows(0, 0, 0, document.Bottom, canvas);
}
base.OnOpenDocument(writer, document);
}
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
PdfPCell cell;
tabFot.TotalWidth = 300F;
cell = new PdfPCell(new Phrase("Footer"));
tabFot.AddCell(cell);
PdfContentByte canvas = writer.DirectContent;
if (canvas.InternalBuffer.Length > 0)
{
tabFot.WriteSelectedRows(0, 0, 0, document.Bottom, canvas);
}
base.OnEndPage(writer, document);
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
}
}
I suspect it has to do with the MemoryStream, I hope you can help me thank you.