页眉和页脚未使用 iTextSharp 显示
Header And Footer Not Shown With 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的问题,希望大家帮帮忙谢谢
在与 "the original developer of 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;
}
}
我有一个问题是虽然打印了文档正文,但没有显示页眉和页脚
这是我的控制器:
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的问题,希望大家帮帮忙谢谢
在与 "the original developer of 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;
}
}