iTextSharp没有显示页眉和页脚

时间:2015-01-06 14:34:57

标签: asp.net-mvc pdf header itextsharp footer

我有一个问题是,虽然打印文件正文,但不显示页眉和页脚

这是我的控制器:

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;
        }
    }

这是我的覆盖:

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);
    }
}

我怀疑它与MemoryStream有关,我希望你能帮助我谢谢你。

1 个答案:

答案 0 :(得分:0)

与&#34; iText&#34;的原始开发人员谈了几句话。并且无法提供任何解决方案并分析调试消息我找到了解决方案,我决定提出解决方案。

错误是关闭此文档和流后的行byte[] byteInfo = workStream.ToArray();,因此在&#34; OnCloseDocument&#34;事件通过writer.CloseStream = false;避免这种情况我使用资源然后我用workStream.Close();关闭它请原谅我的坏英语

我的控制器动作:

[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;
        }
    }

这是我的重写:

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;
    }
}