Search code examples
asp.net-mvcpdfheaderitextfooter

Header And Footer Not Shown With iTextSharp


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.


Solution

  • After having a few words with "the original developer of iText" and unable to provide any solution and analyzing the debugging message I found the solution and i decided to place my solution.

    the error is that the line byte[] byteInfo = workStream.ToArray(); after closing this document and stream, so in "OnCloseDocument" event avoid this with writer.CloseStream = false; I use the resource and then i close this with workStream.Close(); please excuse my bad english

    my controller action:

    [HttpGet]
        public FileStreamResult Report()
        {
            try
            {
                //obtains a dynamic list of a SQL query regardless of the outcome
                List<dynamic> records = HttpContext.Application["registros"] as List<dynamic>;
                //obtains the first record to calculate the number of columns in the table
                Dictionary<string, dynamic> firstRecord = records[0].Properties;
                int columns = firstRecord.Count;
                using (MemoryStream workStream = new System.IO.MemoryStream())
                {
                    using(Document document = new Document(PageSize.LETTER, 20f, 20f, 20f, 20f))
                    {
                        using(PdfWriter writer = PdfWriter.GetInstance(document, workStream))
                        {
                            writer.PageEvent = new PageEventHelper();
                            PdfDestination pdfDest = new PdfDestination(PdfDestination.FIT, 0, document.PageSize.Height, 0);
                            Font helvetica = new Font(Font.FontFamily.HELVETICA, 9f);
    
                            document.Open();
                            PdfPTable table = new PdfPTable(columns);
                            table.WidthPercentage = 100;
                            //I lack still to define in dynamic way
                            float[] widths = new float[] { 45f, 130f, 100f, 70f, 70f, 40f };
                            table.SetWidths(widths);
                            // font of the cell for column names
                            Font dataHeaderFont = FontFactory.GetFont("Helvetica", 9, Font.BOLD, BaseColor.WHITE);
                            //making cells for column names with the first record
                            //"item" is a dictionary with field name and value of the result of the sql query
                            foreach (var item in firstRecord)
                            {
                                PdfPCell cell = new PdfPCell(new Phrase(item.Key.Replace("_", " "), dataHeaderFont));
                                cell.BackgroundColor = BaseColor.BLACK;
                                table.AddCell(cell);
                            }
                            // font of the body table
                            Font dataCellFont = FontFactory.GetFont("Helvetica", 9, Font.NORMAL, BaseColor.BLACK);
                            foreach (var registro in records)
                            {
                                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);
    
                            document.Close();
                            byte[] byteInfo = workStream.ToArray();
                            workStream.Write(byteInfo, 0, byteInfo.Length);
                            workStream.Position = 0;
                        }
    
                        Response.Clear();
                        Response.ContentType = "application/pdf";
                        Response.AppendHeader("Content-disposition", "attachment; filename=Reporte.pdf"); // open in a new window
                        Response.OutputStream.Write(workStream.ToArray(), 0, workStream.ToArray().Length);
                        Response.Flush();
                        workStream.Close();
                    }
                    return new FileStreamResult(workStream, "application/pdf");
                }
            }
            catch (Exception ex)
            {
                string exmsj = ex.Message;
                return null;
            }
        }
    

    this is my overrides:

    public class PageEventHelper : PdfPageEventHelper
    {
        Font ffont = new Font(Font.FontFamily.HELVETICA, 9, Font.NORMAL);
    
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);
        }
        //
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            PdfContentByte cb = writer.DirectContent;
            Phrase header = new Phrase("this is a header", ffont);
            Phrase footer = new Phrase("this is a footer", ffont);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER,
                    header,
                    (document.Right - document.Left) / 2 + document.LeftMargin,
                    document.Top + 10, 0);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER,
                    footer,
                    (document.Right - document.Left) / 2 + document.LeftMargin,
                    document.Bottom - 10, 0);
        }
    
        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            base.OnCloseDocument(writer, document);
            // important to avoid closing the stream, YES!
            writer.CloseStream = false;
        }
    }