2015-01-06 2 views
0

У меня есть проблема в том, что хотя печати тело документа, не показывает верхние и нижние колонтитулыверхние и нижние колонтитулы не показаны с iTextSharp

это мой контроллер:

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, надеюсь, вы можете мне помочь.

ответ

0

После нескольких слов с «оригинальным разработчиком iText» и не смог предоставить какое-либо решение и проанализировав сообщение отладки, я нашел решение, и я решил разместить свое решение.

ошибка в том, что линия byte[] byteInfo = workStream.ToArray(); после закрытия этого документа и потока, так что в случае «OnCloseDocument» избежать этого с 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; 
    } 
} 
+0

это не ASP .NET MVC, ни делая запрос с AJAX, если вы не знаете тему, по крайней мере, не делайте удовольствия. насколько легко было бы запланировать, только если бы был способ сделать что-то, независимо от используемой платформы или окружающей среды, это был бы лучший мир, конечно, это будет называться Narnia. –

+0

HAND и удачи с вашей репутацией. –

Смежные вопросы