Kshitij Bali

Kshitij Bali

  • NA
  • 2
  • 1.5k

How to add page number in C# generated PDF. Format "Page 1of n"

Sep 21 2020 4:32 AM
In my C# code which publishes certain data into PDF, I am looking for a way to put page number in the footer (left, center or right indexed) in the format "Page 1 of n", n being the total number of pages.
Below is my code.
  1. using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "PDF file|*.pdf", ValidateNames = true })
  2. {   
  3.     if (sfd.ShowDialog() == DialogResult.OK)   
  4.     {  
  5.         iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4.Rotate(), 50f, 50f, 50f, 50f);   
  6.         {  
  7.             try   
  8.             {   
  9.                 //Main pdf publishing code follows  
  10.             }   
  11.             catch (Exception ex)   
  12.             {  
  13.                 MessageBox.Show(ex.Message, "Error! try again.", MessageBoxButtons.OK, MessageBoxIcon.Error);   
  14.             }   
  15.             finally   
  16.             {  
  17.                 doc.Close();   
  18.             }  
  19.         }   
  20.     }   
  21. }

Answers (4)