Free Support Forum - aspose.com

Not able to print current page number using $p of $P

Hi,

We are using following code to print current page number out of total pages in footer or header of each page in PDF but it always displays 1 of 6 when there are 6 page

Please help us resolving this issue

        Font font = FontRepository.FindFont("Arial"); 
        Document pdfDocument;

        new Aspose.Pdf.License().SetLicense(@"C:\AsposeLicence\Aspose.Total.lic");

        pdfDocument = new Document();
        
        var page1 = pdfDocument.Pages.Add();
        var page2 = pdfDocument.Pages.Add();
        var page3 = pdfDocument.Pages.Add();
        var page4 = pdfDocument.Pages.Add();
        var page5 = pdfDocument.Pages.Add();
        var page6 = pdfDocument.Pages.Add();

        Aspose.Pdf.Table table = new Aspose.Pdf.Table();

        table.ColumnWidths = "100";
        Aspose.Pdf.Row row = new Aspose.Pdf.Row();            
        var row1cell1 = row.Cells.Add();
        TextFragment absoluteFooterText = new TextFragment();
        TextSegment footerSegment = new TextSegment("$p of $P");
        absoluteFooterText.Segments.Add(footerSegment);
        row1cell1.Paragraphs.Add(absoluteFooterText);
        table.Rows.Add(row);

        Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();

        footer.Paragraphs.Add(table);
        Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
        margin.Bottom = 100;
        margin.Left = 40;

        foreach (Page page in pdfDocument.Pages)
        {
            page.Footer = footer;
            page.PageInfo.Margin = margin;
        }

        pdfDocument.Save(@"C:\ProtocolReport.pdf");

@aparnabn

Thanks for contacting support.

Please use following modified code snippet to achieve correct output. We have also attached an output PDF for your reference.

Aspose.Pdf.Text.Font font = FontRepository.FindFont("Arial");
new Aspose.Pdf.License().SetLicense(@"C:\AsposeLicence\Aspose.Total.lic");
Document pdfDocument = new Document();

var page1 = pdfDocument.Pages.Add();
var page2 = pdfDocument.Pages.Add();
var page3 = pdfDocument.Pages.Add();
var page4 = pdfDocument.Pages.Add();
var page5 = pdfDocument.Pages.Add();
var page6 = pdfDocument.Pages.Add();

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

table.ColumnWidths = "100";
Aspose.Pdf.Row row = new Aspose.Pdf.Row();
var row1cell1 = row.Cells.Add();
TextFragment absoluteFooterText = new TextFragment();
TextSegment footerSegment = new TextSegment("$p of $P");
absoluteFooterText.Segments.Add(footerSegment);
row1cell1.Paragraphs.Add(absoluteFooterText);
table.Rows.Add(row);
            
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Bottom = 100;
margin.Left = 40;

foreach (Page page in pdfDocument.Pages)
{
 Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();
 footer.Paragraphs.Add(table);
 page.Footer = footer;
 page.PageInfo.Margin = margin;
}

pdfDocument.Save(dataDir + @"ProtocolReport.pdf"); 

In case of further assistance, please feel free to let us know.