As stated by @bradbury9, an OnEndPage event is what you're aiming for.
Here's an example of the code that will add the desired footer text in the desired page position every time the OnPageEnd event is triggered
public class MyPdfPageEventHandler: PdfPageEventHelper
{
const float horizontalPosition = 0.5f; // %50 of the page width, starting from the left
const float verticalPosition = 0.1f; // %10 of the page height starting from the bottom
public override void OnEndPage(PdfWriter writer, Document document)
{
var footerText = new Phrase(writer.CurrentPageNumber.ToString());
float posX = writer.PageSize.Width * horizontalPosition;
float posY = writer.PageSize.Height * verticalPosition;
float rotation = 0;
ColumnText.ShowTextAligned(writer.DirectContent, Element.PHRASE, footerText, posX, posY, rotation);
}
}
And here's some sample code on how to make it work
static void Main(string[] args)
{
FileStream fs = new FileStream("NewDocument.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
writer.PageEvent = new MyPdfPageEventHandler(); //This will trigger the code above
doc.Open();
doc.Add(new Paragraph("First Page"));
doc.NewPage();
doc.Add(new Paragraph("Second Page"));
doc.NewPage();
doc.Add(new Paragraph("Thid Page"));
doc.Close();
}
You can use the MyPdfPageEventHandler class to override other page events such as OnStartPage and such.