hi there, does anybody know how I can retrieve the current page number and the page count for a pdf document. i’m currently using the apose.pdf namespace and the DOM in version 8.3.1.0. I already have a Aspose.PDF.HeaderFooter object with custom content on it and I don’t want to use a stamp.
I found the solution, its essentially using a symbol identifier like so Page $p of $P
Hi Henry,
Thanks for using our products.
$p symbol is used to get the current page count and $P is used to get the total page count of PDF files but these features work when using Aspose.Pdf.Generator namespace. When using Aspose.Pdf namespace, you may try using the following code snippet to get the total page count of PDF file.
[C#]
//open document
Document pdfDocument = new Document("c:/pdftest/PageCount_Paoutput.pdf");
Console.WriteLine(pdfDocument.Pages.Count);
In the event of any further query, please feel free to contact.
This does not work in my case. Inside OnBeforePageGenerate
I’m trying to write $"Page {page.Number} of {doc.Pages.Count}"
but all the pages are “Page 1 of 1”, “Page 2 of 2”, “Page 3 of 3”, etc. I also tried passing the page count into the function but that also failed. I’m using Aspose.PDF.
Are you using 24.10 version of the API? Can you please share complete sample code to reproduce the issue so that we can address it accordingly?
I was on 24.8, but trying 24.10 right now and it doesn’t work either. There are two strategies I’ve tried that yield different results, as shown below:
//Generate a page that spans multiple pages.
//...
page.OnBeforePageGenerate += p => GetFooter(document, p)
then:
private void GetFooter(Document doc, Page page){
Table footerGrid new Table();
//Fill footerGrid with unrelated information
///...
TextFragment text = new($"Page {page.Number} of {doc.Pages.Count}");
footerGrid.Rows[1].Cells[2].Paragraphs.Add(text);
HeaderFooter footer = new();
footer.Paragraphs.Add(footerGrid);
page.Footer = footer;
}
this is the process that yields “Page 1 of 1”, “Page 2 of 2”, etc.
The other method I tried was assigning the page count outside of the GetFooter()
function and passing it in, as such:
//Generate a page that spans multiple pages.
//...
long pageCount = document.Pages.Count;
page.OnBeforePageGenerate += p => GetFooter(pageCount, p)
then in GetFooter()
TextFragment text = new($"Page {page.Number} of {pageCount}");
instead of TextFragment text = new($"Page {page.Number} of {doc.Pages.Count}");
When I do it this way, the page count is always 1, like “Page 1 of 1”, “Page 2 of 1”, etc
In the second approach, can you please try using Document.ProcessParagraphs() method before printing the Page numbers and let us know what results did you get? We will further proceed accordingly.
When I do that, the header and footer disappear completely from all pages.
document.ProcessParagraphs();
long pageCount = document.Pages.Count;
page.OnBeforePageGenerate += p => GetFooter(pageCount, p)
In that case, can you please share minimal code sample that we can execute in our environment without any error to reproduce the issue that you are facing? The code snippet you shared earlier is not complete and has missing parts.
public void GetFooter(long pageCount, Page page){
HeaderFooter footer = new();
Table footerGrid = new Table();
Row row = footerGrid.Rows.Add();
Cell cell = row.Cells.Add();
TextFragment text = new($"Page {page.Number} of {pageCount}");
cell.Paragraphs.Add(text);
footer.Paragraphs.Add(footerGrid);
page.Footer = footer;
}
public void ExampleDoc()
{
string filepath = null;
Document document = new Document();
Page page = document.Pages.Add();
foreach(var i in Enumerable.Range(0,100))
page.Paragraphs.Add(new TextFragment($"Text {i}"));
document.ProcessParagraphs();
long pageCount = document.Pages.Count;
page.OnBeforePageGenerate += p => GetFooter(pageCount, p);
using FileStream ms = new FileStream(filepath, FileMode.Create);
document.Save(ms);
}
Please note that you will have to add your own file path to the given code.
To reiterate:
- As it is, the header does not show up.
- Upon removing
document.ProcessParagraphs()
, it will always show a page count of 1. - When looking at
document.Pages.Count
without first assigning it to another variable, the page count returns the current page number.