Página X de Y problema

Intenté 3 formas diferentes de mostrar los números de página,OnCloseDocument el contenido no se muestra en la página, ninguno de ellos funcionó.

My Intention está mostrandoNúmero de página Me gusta est

1 de 10
2 0f 10
..............
............
10 de 10 en cada página

Sé cómo mostrar

1
2
3
4
....
10

pero no sé cómo mostrar el número total de páginas

Estoy usando OnCloseDocument para mostrar el número de páginas contadas, pero la El contenido no se muestra.

public class MyPdfPageEventHelpPageNo : iTextSharp.text.pdf.PdfPageEventHelper
{
    protected PdfTemplate total;
    protected BaseFont helv;
    private bool settingFont = false;

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

        bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    }
    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
    //See below
    }

PRIMERA MANERA:

    public override void OnCloseDocument(PdfWriter writer, Document document)     
    {
        //I create a table with one column like below.
        PdfPTable pageNumber2 = new PdfPTable(1);
        pageNumber2.TotalWidth = 50;
        pageNumber2.HorizontalAlignment = Element.ALIGN_RIGHT;

        pageNumber2.AddCell(BuildTable2RightCells("Page " + writer.PageNumber));
        pageNumber.AddCell(BuildTable2LeftCells(writer.PageCount));
        pageNumber2.WriteSelectedRows(0, -1, 500, 
        (document.PageSize.GetBottom(140)), cb);
    }    

2ª MANERA:

    public override void OnCloseDocument(PdfWriter writer, Document document)     
    {
        ColumnText.ShowTextAligned(template,Element.ALIGN_CENTER,new
        Phrase(writer.PageNumber.ToString()), 500, 140, 0);
    }

3ª MANERA:

    public override void OnCloseDocument(PdfWriter writer, Document document)     
    {
        template.BeginText();
        template.SetFontAndSize(bf, 8);
        template.SetTextMatrix(500, 140);
        template.ShowText(Convert.ToString((writer.PageNumber - 1)));
        template.EndText();
    }

¿Estoy haciendo algo mal

Respuestas a la pregunta(4)

Su respuesta a la pregunta