我在PdfPageEventHelper的文档中添加了页眉和页脚.
该文档在运行时填充了几个“大”表.
我试图简单地通过“documen.Add(table)”添加这些表,但我的页眉和页脚结果被覆盖了.
我已经尝试过两种方法来添加表格(WriteSelectedRows和document.Add(myPdfPtable)).
这是PageEventHelper的代码:
private class MyPageEventHandler : PdfPageEventHelper { public iTextSharp.text.Image ImageHeader { get; set; } public iTextSharp.text.Image ImageFooter { get; set; } public override void OnEndPage(PdfWriter writer,Document document) { var fontintestazione = FontFactory.GetFont("Verdana",10,Font.BOLD,BaseColor.LIGHT_GRAY); var fontRight = FontFactory.GetFont("Verdana",8,BaseColor.WHITE); var fontFooter = FontFactory.GetFont("Verdana",6,Font.NORMAL,BaseColor.BLUE); float cellHeight = document.TopMargin; Rectangle page = document.PageSize; PdfPTable head = new PdfPTable(3); head.TotalWidth = page.Width; PdfPCell c = new PdfPCell(ImageHeader,true); c.HorizontalAlignment = Element.ALIGN_LEFT; c.FixedHeight = cellHeight; c.Border = PdfPCell.NO_BORDER; head.AddCell(c); c = new PdfPCell(new Phrase("somePhrase",fontintestazione)); c.Border = PdfPCell.NO_BORDER; head.AddCell(c); c = new PdfPCell(new Phrase("someTextBlah",fontRight)); c.Border = PdfPCell.NO_BORDER; c.HorizontalAlignment = 1; c.BackgroundColor = new BaseColor(70,130,180); head.AddCell(c); head.WriteSelectedRows(0,-1,page.Height - cellHeight + head.TotalHeight -30,writer.DirectContent); PdfPTable footer = new PdfPTable(2); footer.TotalWidth = 316f; float[] cfWidths = new float[] { 2f,1f }; footer.SetWidths(cfWidths); PdfPCell cf = new PdfPCell(ImageFooter,true); cf.HorizontalAlignment = Element.ALIGN_RIGHT; cf.FixedHeight = cellHeight; cf.Border = PdfPCell.NO_BORDER; footer.AddCell(cf); cf = new PdfPCell(new Phrase("someEndingText",fontFooter)); cf.HorizontalAlignment = Element.ALIGN_LEFT; cf.Border = PdfPCell.NO_BORDER; footer.AddCell(cf); footer.WriteSelectedRows(0,50,writer.DirectContent); }
在我的页面上,我只是这样做:
var document = new Document(PageSize.A4); var output = new MemoryStream(); var writer = PdfWriter.GetInstance(document,output); iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/header.ong")); iTextSharp.text.Image imageFooter = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/footer.png")); MyPageEventHandler eve = new MyPageEventHandler { ImageHeader = imageHeader,ImageFooter = imageFooter }; writer.PageEvent = eve; document.Open(); //adding a table PdfPTable cvTable = new PdfPtable(3); cvTable.TotalWidth = document.PageSize.Width; PdfPCell hCell = new PdfPCell(new Phrase("Jobs By User",aCustomFont)); cvTable.AddCell(hCell); for(int i = 0; i < myTable.Records.Count; i++) { PdfPCell idCell = new PdfPCell(new Phrase(myTable.Records[i]._id,aFont)); cvTable.Add(idCell); //same stuff for other fields of table } //first attempt.... Failed: document.Add(cvTable) //<- header and footer are overwritten by table //second attempt..... Failed too... cvTable.WriteSelectedRows(0,myPoisition,writer.DirectContent); //kind of fail...: //the table is large and need more pages. It is trunked on the first page and overwrite //the footer.
解决方法
在你的OnEndPage方法中你有这一行:
head.WriteSelectedRows(0,page.Height - cellHeight + head.TotalHeight - 30,writer.DirectContent);
该代码根据页面的高度和上边距正确计算放置内容的位置,但也包含一个神奇的30,这导致标题绘制在表格的顶部.将其更改为此标题即可.
head.WriteSelectedRows(0,page.Height - cellHeight + head.TotalHeight,writer.DirectContent);
我猜这30是试图在你的标题和表本身之间加入一些填充.我建议实际上是在主代码中更改文档的边距:
document.SetMargins(document.LeftMargin,document.RightMargin,document.TopMargin + 30,document.BottomMargin);
然后在OnEndPage方法中计算:
float cellHeight = document.TopMargin - 30;
您的页脚代码实际上并不考虑底部边距,只是将其绘制为50,因此这将始终重叠.快速解决方法是将其更改为:
footer.WriteSelectedRows(0,footer.TotalHeight,writer.DirectContent);
这至少会使页脚底部对齐.如果你想要一些像上面那样的填充,只需再次调整文档边距:
document.SetMargins(document.LeftMargin,document.BottomMargin + 30);