2012-03-25 60 views
6

我在PdfPageEventHelper的文檔中添加了頁眉和頁腳。使用iTextSharp的頁眉,頁腳和大型表格

該文件在運行時填充了幾個「大」表。

我試着簡單地通過「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, Font.BOLD, 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, 10, 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, -1, 10, 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, -1, 10, 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. 

回答

13

在你OnEndPage方法你有這樣一行:

head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight - 30, writer.DirectContent); 

該代碼根據頁面的高度和頂部邊距正確計算了放置內容的位置,但也包含一個神奇的30,導致頭部被繪製在表格頂部。將其更改爲此,您的標題將會很好。

head.WriteSelectedRows(0, -1, 10, 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, -1, 10, footer.TotalHeight, writer.DirectContent); 

這將至少獲得頁腳底部對齊。如果你想要一些像上面一樣的填充,只需再次調整文檔邊距:

document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin + 30); 
+0

你又幫了我一次。謝謝克里斯! :d – 2013-02-14 20:58:15