2010-10-19 104 views
0

我正在嘗試使用ITextSharp庫5.0.4編寫頁腳,並且無法打印頁碼,因爲OnEndPage和OnClosePage未被調用。如何使用iTextSharp編寫頁腳5.0.4

這是一個簡單的控制檯應用程序,其中我打印表格以生成幾頁,並期望OnEndPage或OnClosePage被調用,一旦document.close()方法執行後,腳本代碼將被執行。

請讓我知道這有什麼問題嗎?

+0

我們可以看到你的一些代碼嗎? – 2010-10-19 13:46:19

回答

2

您需要創建一個擴展PdfPageEventHelper的類,並覆蓋OnStartPage和OnEndPage。使用此幫助程序類來處理PdfWriter中的PageEvent

Massoud Mazar的博客文章Code sample for using iTextSharp PDF library有一個很好的例子。

如果你這樣做,仍然有問題,請張貼一些代碼。

+0

感謝這篇文章。我已經提到這個博客,只有剩下的東西是分配writer.pageevents – AHK 2010-10-21 06:32:32

+0

writer.PageEvent = PageEventHandler; – AHK 2010-10-21 06:32:51

3

嗯,我發現如何創建與頁碼和圖像沒有併發症的頁腳。我使用itextsharp 5.1.2。這很容易。 第1步:創建一個App_Code文件文檔的.cs與名字......在我的情況「pdfPage.cs」 第2步:將此代碼複製並粘貼到裏面:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Text; 
using System.IO; 
using System.Xml; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 
using iTextSharp.text.html.simpleparser; 
using System.Collections; 
using System.Net; 


namespace myApp.ns.pages 

{ 
    public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper 
    { 

     public override void OnEndPage(PdfWriter writer, Document doc) 
     { 

      BaseColor grey = new BaseColor(128, 128, 128); 
      Font font = FontFactory.GetFont("Arial", 9, Font.NORMAL, grey); 
      //tbl footer 
      PdfPTable footerTbl = new PdfPTable(1); 
      footerTbl.TotalWidth = doc.PageSize.Width; 
      //img footer 
      iTextSharp.text.Image foot= iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("footer.jpg")); 
      foot.ScalePercent(45); 

      footerTbl.HorizontalAlignment = Element.ALIGN_CENTER; 
      PdfPCell cell = new PdfPCell(foot); 
      cell.HorizontalAlignment = Element.ALIGN_CENTER ; 
      cell.Border = 0; 
      footerTbl.AddCell(cell); 


      //page number 
      Chunk myFooter = new Chunk("Page " + (doc.PageNumber), FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8, grey)); 
      PdfPCell footer = new PdfPCell(new Phrase(myFooter));  
      footer.Border = Rectangle.NO_BORDER;  
      footer.HorizontalAlignment = Element.ALIGN_CENTER; 
      footerTbl.AddCell(footer); 

     //this is for the position of the footer ... im my case is "+80" 
     footerTbl.WriteSelectedRows(0,-1, 0, (doc.BottomMargin + 80), writer.DirectContent); 
     } 

    } 

} 

並保存。

最後一步:寫入.cs文件,在開始時創建pdf「using myApp.ns.pages;」。

這就是全部