16

I am using iTextSharp DLL in ASP.NET code. I am fetching a data into dataset and Add the dataset into PDF table.

If my dataset have more 100 rows then 100 rows will be added into PDF table and 3 or 4 page will be created in PDF file.

How can I add the footer in each page?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
himanshu soni
  • 161
  • 1
  • 1
  • 3

5 Answers5

23

You need to inherit from PdfPageEventHelper class and override the methods shown in the code snippet below:

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();
        }
    }
bleeeah
  • 3,534
  • 19
  • 25
  • 2
    if you don't see the page number, don't forget to set your document.bottomMargin, or even add (float)50 to it – Sebastien H. Jun 28 '13 at 08:20
  • I guess this doesn't work anymore? this.RunDateFont doesn't exist. – Justin Oct 04 '16 at 00:49
  • this.RunDateFont is not part of iText but a font variable defined in the outer class. – bleeeah Oct 04 '16 at 12:13
  • @bleeeah can you comment on why you subtract one from PageNumber? I used to do that and recently it started giving incorrect results, was there a bug in iTextSharp that has been fixed in newer versions? – Betty Crokker May 31 '17 at 14:36
  • I know the question is really old and Betty Crokker's follow up question is old too. But I ran into this on some older code I was maintaining and I believe, for those still using version 5, there was a change between 5.5.7 and 5.5.8 in the Close() method of PdfDocument, which removed an extra call to NewPage() (which increments page count). With one less NewPage() call, you no longer need to subtract 1 from PageNumber - if doing something in OnCloseDocument, of course . I gather version 7 does this all completely differently. – Peter R Jan 22 '18 at 17:01
  • 1
    it only add page number (Y) in string page X of Y at the last page. I cant see total pages on start page. It shows (page 1 of ) thats it. – Ali Exalter Mar 21 '18 at 14:43
2

If you just need a simple page number, then this does the job:

public class PageHeaderFooter : PdfPageEventHelper
{
    private readonly Font _pageNumberFont = new Font(Font.HELVETICA, 8f, Font.NORMAL, Color.BLACK);

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        AddPageNumber(writer, document);
    }

    private void AddPageNumber(PdfWriter writer, Document document)
    {
        var text = writer.PageNumber.ToString();

        var numberTable = new PdfPTable(1);
        var numberCell = new BorderlessPdfPCell(new Phrase(text, _pageNumberFont)) {HorizontalAlignment = Element.ALIGN_RIGHT};

        numberTable.AddCell(numberCell);
        numberTable.TotalWidth = 50;
        numberTable.WriteSelectedRows(0, -1, document.Right - 80, document.Bottom + 20, writer.DirectContent);
    }
}

And you use it this way:

var stream = new MemoryStream();
var document = new Document(PageSize.A4, 20, 20, 30, 30);

var pdfWriter = PdfWriter.GetInstance(document, stream);
pdfWriter.PageEvent = new PageHeaderFooter();
// rest of PDF generation
Episodex
  • 4,479
  • 3
  • 41
  • 58
1

You must inherit pageeventhepler to add page number to dynamically generated pdf pages.

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();
    }
}
0

Here is another approach, make templates on each page and then at onCloseDocument update those templates look at the code below I am using iTextSharp 7

 class PDFBackgroundHelper : PdfPageEventHelper
{

    private PdfContentByte cb;
    private List<PdfTemplate> templates;
    //constructor
    public PDFBackgroundHelper()
    {
        this.templates = new List<PdfTemplate>();
    }

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

        cb = writer.DirectContentUnder;
        PdfTemplate templateM = cb.CreateTemplate(50, 50);
        templates.Add(templateM);

        int pageN = writer.CurrentPageNumber;
        String pageText = "Page " + pageN.ToString() +" of ";
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        float len = bf.GetWidthPoint(pageText, 10);
        cb.BeginText();
        cb.SetFontAndSize(bf, 10);
        cb.SetTextMatrix(document.LeftMargin, document.PageSize.GetBottom(document.BottomMargin));
        cb.ShowText(pageText);
        cb.EndText();
        cb.AddTemplate(templateM, document.LeftMargin + len, document.PageSize.GetBottom(document.BottomMargin));
    }

    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
        base.OnCloseDocument(writer, document);
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        foreach (PdfTemplate item in templates)
        {
            item.BeginText();
            item.SetFontAndSize(bf, 10);
            item.SetTextMatrix(0, 0);
            item.ShowText("" + (writer.PageNumber));
            item.EndText();
        }

    } 
Ali Exalter
  • 420
  • 2
  • 8
  • 13
0
    Hello This code is for itextsharp 5.5 c# desktop application
 using System;
     using System.Windows.Forms;
     using MySql.Data.MySqlClient;
     using System.IO;
     
     using iTextSharp;
     using iTextSharp.text;
     using iTextSharp.text.pdf;
     
     
     namespace Stock
     {
         public partial class EditGuardsActivity : Form
         {
     
     
     
             Databaseconnection connect = new Databaseconnection();
             public EditGuardsActivity()
             {
                 InitializeComponent();
     
             }
     
             private void saveedit_Click(object sender, EventArgs e)
             {
     
     
     
     
     
     
                 if (table.Text != "" )
                 {
                     try
                     {
                         //This is my connection string i have assigned the database file address path
     
                         //This is my update query in which i am taking input from the user through windows forms and update the record.
                         string Query = "update table.employees set lastname='" + this.lastname.Text + "',firstname='" + this.firstname.Text + "',dateofbirth='" + this.dateofbirth.Text + "',idno='" + this.idnumber.Text + "',languege='" + this.language.Text + "',nssf='" + this.nssf.Text + "',phonenumber='" + this.phonenumber.Text + "',country='" + this.country.Text + "',fiscaladdress='" + this.editfiscaladress.Text + "',card_number='" + this.cardnumber.Text + "'where forcenumber='" + this.table.Text + "';";
                         //This is  MySqlConnection here i have created the object and pass my connection string.
                         MySqlCommand MyCommand2 = new MySqlCommand(Query, connect.MyCon);
                         MySqlDataReader MyReader2;
                         connect.MyCon.Open();
                         MyReader2 = MyCommand2.ExecuteReader();
                         UsersonWeb allusers = new UsersonWeb();
                         allusers.Close();
                         MessageBox.Show("Data Updated");
                         this.Close();
                         connect.MyCon.Close();//Connection closed here
                     }
                     catch (Exception ex)
                     {
                         MessageBox.Show(ex.Message);
                     }
     
     
                 }
             }
     
             private void printDocumentGuards_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
             {
     
     
     
     
     
                 //e.Graphics.DrawString("Total Amount:      £" + TotalAmountTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 30));
                 //e.Graphics.DrawString("Sales Tax (16%): £" + SalesTaxTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 60));
                 //e.Graphics.DrawString("Total To Pay:       £" + TotalToPayTextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(550, yPos + 90));
     
                 // reset the variables
     
     
     
             }
     
             private void printdetailguards_Click(object sender, EventArgs e)
             {
     
                 string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                 string filepath = path + "/GMSPDF/" + txt.Text.Trim() + ".pdf";
                 String amasezerano1 = "Date: " + DateTime.Now.ToShortDateString() + "\n" +
                    "FIRST NAME: " + firstname.Text.Trim() + "\n" +
                    "LAST NAME: " + lastname.Text.Trim() + "\n" +
                    "GUARD FORCE: " + guardforcenmbr.Text.Trim() + 
                     "IMPAMVU: AMASEZERANO" + "\n" +
                 "\n\nIcyo amategeko y’ umurimo ateganya  Itegeko ry’umurimo riteganya " +
                  "ibi bikurikira mu guhagarika by’\n agateganyo amasezerano y’ akazi:Ingingo " +
                 "ya 18: Guhagarika by’ agateganyo amasezerano y’ akazi - 5 º " +
                 "Ikigo gihagaritse imirimo yacyo by’igihe gito bitewe n'impamvu" +
                 " z’ubukungu cyangwa za tekiniki; - 6 º Hari impamvu ndakumirwa zituma" +
                 " imirimo y’ikigo ihagarara (force majeure).\n Mu bihe nk’ ibyo," +
                 "  ni byiza kumenyesha abakozi ihagarikwa by’ agateganyo ry’amasezerano y’" +
                 " akazi kugera igihe ibyo bihe (ibyo ari byo byose) bizarangirira." +
                 "Ni gute wabyitwaramo: 1.Andikira abakozi ibaruwa ibamenyesha " +
                 "ko kubera ibihe bikomeye mu bukungu n’impamvu ntakumirwa, " +
                 "ikigo cyafashe icyemezo cyo guhagarika by’2.Hamagara buri " +
                 "mukozi ku giti cye umusobanurire uko ibintu bimeze n’ " +
                 "impamvu ikigo kidashoboye gukomeza kumuhemba " +
                 "umushahara we.3.Ku bigo bigishoboye kuba byatanga " +
                 "imishahara y’ abakozi, ingoboka zimwe na zimwe(ziyongera ku mushahara)" +
                 " urugero nk’ ingoboka y’ ingendo, yo guhamagara kuri telefoni n’ ibindi" +
                 " zishobora kugabanywa cyangwa zikanavanwaho mu gihe abakozi baba" +
                 " batagikora na gato.Mu bihe by’ ihagarikwa ry’ agateganyo: " +
                 "-Ntibyitezwe ko ikigo kigomba gukomeza guhemba abakozi " +
                 "-Ntibivuze ko ikigo cyirukanye abakozi mu bihe nk’ibi - " +
                 "Niba ikigo kigishoboye gufasha bakozi kibagenera ingoboka nto buri kwezi," +
                 " byaba ari ingenzi.Muzirikane ko abakozi bari batunzwe n’ umushahara gusa." +
                 "\n\nIcyo amategeko y’ umurimo ateganya  Itegeko ry’umurimo riteganya " +
                  "ibi bikurikira mu guhagarika by’\n agateganyo amasezerano y’ akazi:Ingingo " +
                 "ya 18: Guhagarika by’ agateganyo amasezerano y’ akazi - 5 º " +
                 "Ikigo gihagaritse imirimo yacyo by’igihe gito bitewe n'impamvu" +
                 " z’ubukungu cyangwa za tekiniki; - 6 º Hari impamvu ndakumirwa zituma" +
                 " imirimo y’ikigo ihagarara (force majeure).\n Mu bihe nk’ ibyo," +
                 "  ni byiza kumenyesha abakozi ihagarikwa by’ agateganyo ry’amasezerano y’" +
                 " akazi kugera igihe ibyo bihe (ibyo ari byo byose) bizarangirira." +
                 "Ni gute wabyitwaramo: 1.Andikira abakozi ibaruwa ibamenyesha " +
                 "ko kubera ibihe bikomeye mu bukungu n’impamvu ntakumirwa, " +
                 "ikigo cyafashe icyemezo cyo guhagarika by’2.Hamagara buri " +
                 "mukozi ku giti cye umusobanurire uko ibintu bimeze n’ " +
                 "impamvu ikigo kidashoboye gukomeza kumuhemba " +
                 "umushahara we.3.Ku bigo bigishoboye kuba byatanga " +
                 "imishahara y’ abakozi, ingoboka zimwe na zimwe(ziyongera ku mushahara)" +
                 " urugero nk’ ingoboka y’ ingendo, yo guhamagara kuri telefoni n’ ibindi" +
                 " zishobora kugabanywa cyangwa zikanavanwaho mu gihe abakozi baba" +
                 " batagikora na gato.Mu bihe by’ ihagarikwa ry’ agateganyo: " +
                 "-Ntibyitezwe ko ikigo kigomba gukomeza guhemba abakozi " +
                 "-Ntibivuze ko ikigo cyirukanye abakozi mu bihe nk’ibi - Niba ikigo" +
                 " kigishoboye gufasha bakozi kibagenera ingoboka nto buri kwezi," +
                 " byaba ari ingenzi.Muzirikane ko abakozi bari batunzwe n’ umushahara gusa." +
                 "Icyo amategeko y’ umurimo ateganya  Itegeko ry’umurimo riteganya " +
                  "ibi bikurikira mu guhagarika by’ agateganyo amasezerano y’ akazi:Ingingo " +
                 "ya 18: Guhagarika by’ agateganyo amasezerano y’ akazi - 5 º " +
                 "Ikigo gihagaritse imirimo yacyo by’igihe gito bitewe n'impamvu" +
                 " z’ubukungu cyangwa za tekiniki; - 6 º Hari impamvu ndakumirwa zituma" +
                 " imirimo y’ikigo ihagarara (force majeure).Mu bihe nk’ ibyo," +
                 "  ni byiza kumenyesha abakozi ihagarikwa by’ agateganyo ry’amasezerano y’" +
                 " akazi kugera igihe ibyo bihe (ibyo ari byo byose) bizarangirira." +
                 "Ni gute wabyitwaramo: 1.Andikira abakozi ibaruwa ibamenyesha " +
                 "ko kubera ibihe bikomeye mu bukungu n’impamvu ntakumirwa, " +
                 "ikigo cyafashe icyemezo cyo guhagarika by’2.Hamagara buri " +
                 "mukozi ku giti cye umusobanurire uko ibintu bimeze n’ " +
                 "impamvu ikigo kidashoboye gukomeza kumuhemba " +
                 "umushahara we.3.Ku bigo bigishoboye kuba byatanga " +
                 "imishahara y’ abakozi, ingoboka zimwe na zimwe(ziyongera ku mushahara)" +
                 " urugero nk’ ingoboka y’ ingendo, yo guhamagara kuri telefoni n’ ibindi" +
                 " zishobora kugabanywa cyangwa zikanavanwaho mu gihe abakozi baba" +
                 " batagikora na gato.Mu bihe by’ ihagarikwa ry’ agateganyo: " +
                 "-Ntibyitezwe ko ikigo kigomba gukomeza guhemba abakozi " +
                 "-Ntibivuze ko ikigo cyirukanye abakozi mu bihe nk’ibi - Niba ikigo kigishoboye gufasha bakozi kibagenera " +
                 "ingoboka nto buri kwezi, byaba ari ingenzi.Muzirikane ko abakozi bari batunzwe n’ umushahara gusa." +
                 "Icyo amategeko y’ umurimo ateganya  Itegeko ry’umurimo riteganya " +
                  "ibi bikurikira mu guhagarika by’ agateganyo amasezerano y’ akazi:Ingingo " +
                 "ya 18: Guhagarika by’ agateganyo amasezerano y’ akazi - 5 º " +
                 "Ikigo gihagaritse imirimo yacyo by’igihe gito bitewe n'impamvu" +
                 " z’ubukungu cyangwa za tekiniki; - 6 º Hari impamvu ndakumirwa zituma" +
                 " imirimo y’ikigo ihagarara (force majeure). Mu bihe nk’ ibyo," +
                 "  ni byiza kumenyesha abakozi ihagarikwa by’ agateganyo ry’amasezerano y’" +
                 " akazi kugera igihe ibyo bihe (ibyo ari byo byose) bizarangirira." +
                 "Ni gute wabyitwaramo: 1.Andikira abakozi ibaruwa ibamenyesha " +
                 "ko kubera ibihe bikomeye mu bukungu n’impamvu ntakumirwa, " +
                 "ikigo cyafashe icyemezo cyo guhagarika by’2.Hamagara buri " +
                 "mukozi ku giti cye umusobanurire uko ibintu bimeze n’ " +
                 "impamvu ikigo kidashoboye gukomeza kumuhemba " +
                 "umushahara we.3.Ku bigo bigishoboye kuba byatanga " +
                 "imishahara y’ abakozi, ingoboka zimwe na zimwe(ziyongera ku mushahara)" +
                 " urugero nk’ ingoboka y’ ingendo, yo guhamagara kuri telefoni n’ ibindi" +
                 " zishobora kugabanywa cyangwa zikanavanwaho mu gihe abakozi baba" +
                 " batagikora na gato.Mu bihe by’ ihagarikwa ry’ agateganyo: " +
                 "-Ntibyitezwe ko ikigo kigomba gukomeza guhemba abakozi " +
                 "-Ntibivuze ko ikigo cyirukanye abakozi mu bihe nk’ibi - Niba ikigo kigishoboye gufasha bakozi kibagenera ingoboka nto buri kwezi, byaba ari ingenzi.Muzirikane ko abakozi bari batunzwe n’ umushahara gusa.";
     
     
     
     
     
     
     
     
                 using (FileStream msReport = new FileStream(filepath, FileMode.Create))
                 {
                     //step 1
     
                     using (Document pdfDoc = new Document(PageSize.A4, 30f, 30f, 100f, 100f))
                     {
                         try
                         {
                             // step 2
                             PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);
                             pdfWriter.PageEvent = new ITextEvents();
                             pdfWriter.PageEvent = new PDFFooter();
                             //open the stream 
                             pdfDoc.Open();
     
     
                             Paragraph para = new Paragraph(amasezerano1);
                             para.Alignment = Element.ALIGN_LEFT;
     
     
                             pdfDoc.Add(para);
                             pdfDoc.NewPage();
     
     
                             pdfDoc.Close();
                         }
                         catch (Exception ex)
                         {
                             //handle exception
                         }
                         finally
                         {
                         }
                     }
     
     
     
                 }
             }
     
     
     
     
     
     
             public class ITextEvents : PdfPageEventHelper
             {
     
                 iTextSharp.text.Image logoimg = iTextSharp.text.Image.GetInstance(Resources.header2, System.Drawing.Imaging.ImageFormat.Png);
                 iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(Resources.footerpage, System.Drawing.Imaging.ImageFormat.Jpeg);
                 // This is the contentbyte object of the writer
     
                 // This is the contentbyte object of the writer
                 PdfContentByte cb;
     
                 // we will put the final number of pages in a template
                 PdfTemplate headerTemplate, footerTemplate;
     
                 // this is the BaseFont we are going to use for the header / footer
                 BaseFont bf = null;
     
                 // This keeps track of the creation time
                 DateTime PrintTime = DateTime.Now;
     
                 #region Fields
                 private string _header;
                 #endregion
     
                 #region Properties
                 public string Header
                 {
                     get { return _header; }
                     set { _header = value; }
                 }
                 #endregion
     
                 public override void OnOpenDocument(PdfWriter writer, Document document)
                 {
                     try
                     {
                         PrintTime = DateTime.Now;
                         bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                         cb = writer.DirectContent;
                         headerTemplate = cb.CreateTemplate(100, 100);
                         footerTemplate = cb.CreateTemplate(50, 50);
                     }
                     catch (DocumentException de)
                     {
                     }
                     catch (System.IO.IOException ioe)
                     {
                     }
                 }
     
                 public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
                 {
                     base.OnEndPage(writer, document);
                     iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
                     iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
                     Phrase p1Header = new Phrase("Sample Header Here", baseFontNormal);
     
                     //Create PdfTable object
                     PdfPTable pdfTab = new PdfPTable(2);
     
                     //We will have to create separate cells to include image logo and 2 separate strings
                     //Row 1
                     PdfPCell pdfCell1 = new PdfPCell(logoimg);
                     PdfPCell pdfCell2 = new PdfPCell();
                     //Add paging to header
                     //set the alignment of all three cells and set border to 0
                     pdfCell1.HorizontalAlignment = Element.ALIGN_CENTER;
                     pdfCell2.HorizontalAlignment = Element.ALIGN_CENTER;
                     pdfCell1.Border = 0;
                     pdfCell2.Border = 0;
     
                     //add all three cells into PdfTable
                     pdfTab.AddCell(pdfCell1);
                     pdfTab.AddCell(pdfCell2);
     
 
 
 ----------
 
 
                     pdfTab.TotalWidth = document.PageSize.Width - 100f;
                     pdfTab.WidthPercentage = 10;
                     //pdfTab.HorizontalAlignment = Element.ALIGN_CENTER;    
                     //0790316144
                     //call WriteSelectedRows of PdfTable. This writes rows from PdfWriter in PdfTable
                     //first param is start row. -1 indicates there is no end row and all the rows to be included to write
                     //Third and fourth param is x and y position to start writing
                     pdfTab.WriteSelectedRows(0, 10, 10, document.PageSize.Height - 10, writer.DirectContent);
                     //set pdfContent value
     
                 }
     
                 public override void OnCloseDocument(PdfWriter writer, Document document)
                 {
                     iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(Resources.footerpage, System.Drawing.Imaging.ImageFormat.Jpeg);
                     base.OnCloseDocument(writer, document);
                     headerTemplate.BeginText();
                     headerTemplate.SetFontAndSize(bf, 1);
                     headerTemplate.SetTextMatrix(0, 0);
                     headerTemplate.ShowText((writer.PageNumber - 1).ToString());
                     headerTemplate.EndText();
                     footerTemplate.BeginText();
                     footerTemplate.SetFontAndSize(bf, 12);
                     footerTemplate.SetTextMatrix(0, 0);
                     footerTemplate.ShowText((writer.PageNumber - 1).ToString());
                     footerTemplate.EndText();
                 }
             }
             public class PDFFooter : PdfPageEventHelper
             {
                 iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(Resources.footerpage, System.Drawing.Imaging.ImageFormat.Jpeg);
     
                 // write on top of document
                 //public override void OnOpenDocument(PdfWriter writer, Document document)
                 //{
                    
                 //}
     
                 // write on start of each page
                 public override void OnStartPage(PdfWriter writer, Document document)
                 {
                     base.OnStartPage(writer, document);
                 }
     
                 // write on end of each page
                 public override void OnEndPage(PdfWriter writer, Document document)
                 {
                     
                     base.OnEndPage(writer, document);
                     PdfPTable tabFot = new PdfPTable(3);
                     PdfPCell cell;
                     tabFot.TotalWidth = 597F;
                     cell = new PdfPCell();
                     //cell.Border = Rectangle.NO_BORDER;
                     cell.HorizontalAlignment = Element.ALIGN_LEFT;
                     tabFot.AddCell(myImage);
                     tabFot.WriteSelectedRows(0, -2, -2, document.Bottom, writer.DirectContent);
                 }
     
                 //write on close of document
                 public override void OnCloseDocument(PdfWriter writer, Document document)
                 {
                     base.OnCloseDocument(writer, document);
                 }
             }
         }
     
     }
bzsaph
  • 1