2012-05-09 112 views
9

我在ASP.NET代碼中使用iTextSharp DLL。我正在將數據提取到數據集中並將數據集添加到PDF表中。如何使用Itextsharp在PDF頁腳中添加頁碼

如果我的數據集有更多的100行,那麼100行將被添加到PDF表格中,並且3或4頁面將被創建爲PDF文件。

如何在每個頁面中添加頁腳?

+2

重複http://stackoverflow.com/questions/1032614/itextsharp-creating-a-footer-page-of –

回答

16

你需要從PdfPageEventHelper類繼承,並覆蓋在下面的代碼片段顯示的方法:

Document doc = new Document(PageSize.A4.Rotate());   

using (MemoryStream ms = new MemoryStream()) 
{ 
    PdfWriter writer = PdfWriter.GetInstance(doc, ms); 
    PageEventHelper pageEventHelper = new PageEventHelper(); 
    writer.PageEvent = pageEventHelper; 
} 

public class PageEventHelper : PdfPageEventHelper 
    { 
     PdfContentByte cb; 
     PdfTemplate template; 


     public override void OnOpenDocument(PdfWriter writer, Document document) 
     { 
      cb = writer.DirectContent; 
      template = cb.CreateTemplate(50, 50); 
     } 

     public override void OnEndPage(PdfWriter writer, Document document) 
     { 
      base.OnEndPage(writer, document); 

      int pageN = writer.PageNumber; 
      String text = "Page " + pageN.ToString() + " of "; 
      float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size); 

      iTextSharp.text.Rectangle pageSize = document.PageSize; 

      cb.SetRGBColorFill(100, 100, 100); 

      cb.BeginText(); 
      cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size); 
      cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin)); 
      cb.ShowText(text); 

      cb.EndText(); 

      cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin)); 
     } 

     public override void OnCloseDocument(PdfWriter writer, Document document) 
     { 
      base.OnCloseDocument(writer, document); 

      template.BeginText(); 
      template.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size); 
      template.SetTextMatrix(0, 0); 
      template.ShowText("" + (writer.PageNumber - 1)); 
      template.EndText(); 
     } 
    } 
+1

的,如果你沒有看到的頁碼,不要忘記設置您的文檔.bottomMargin,甚至添加(浮動)50到它 – Shide

+0

我想這不工作了? this.RunDateFont不存在。 – Justin

+0

this.RunDateFont不是iText的一部分,而是在外部類中定義的字體變量。 – bleeeah

-1

你必須繼承pageeventhepler添加到動態生成的頁面數量的PDF頁面。

public void printPdf() 
{ 
    Document doc = new Document(PageSize.A4.Rotate()); 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     PdfWriter writer = PdfWriter.GetInstance(doc, ms); 
     PageEventHelper pageEventHelper = new PageEventHelper(); 
     writer.PageEvent = pageEventHelper; 
    } 
} 

public class PageEventHelper : PdfPageEventHelper 
{ 
    PdfContentByte cb; 
    PdfTemplate template; 


    public override void OnOpenDocument(PdfWriter writer, Document document) 
    { 
     cb = writer.DirectContent; 
     template = cb.CreateTemplate(50, 50); 
    } 

    public override void OnEndPage(PdfWriter writer, Document document) 
    { 
     base.OnEndPage(writer, document); 

     int pageN = writer.PageNumber; 
     String text = "Page " + pageN.ToString() + " of "; 
     float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size); 

     iTextSharp.text.Rectangle pageSize = document.PageSize; 

     cb.SetRGBColorFill(100, 100, 100); 

     cb.BeginText(); 
     cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size); 
     cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin)); 
     cb.ShowText(text); 

     cb.EndText(); 

     cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin)); 
    } 

    public override void OnCloseDocument(PdfWriter writer, Document document) 
    { 
     base.OnCloseDocument(writer, document); 

     template.BeginText(); 
     template.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size); 
     template.SetTextMatrix(0, 0); 
     template.ShowText("" + (writer.PageNumber - 1)); 
     template.EndText(); 
    } 
} 
相關問題