We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to add `PageNumberStamp` while creating new PDF file by HTML String in C#?

I get following code from Aspose.Pdf documentation for add PageNumberStamp on existing pdf file.

Document pdfDocument = new Document("output.pdf");
PageNumberStamp pageNumberStamp = new PageNumberStamp();
pageNumberStamp.Background = false;
pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;
pageNumberStamp.BottomMargin = 10;
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center;
pageNumberStamp.StartingNumber = 1;

I want to add PageNumberStamp while creating new pdf file by HTML string.
i tried many different ways but i can’t do this. Please help me that how i can create PDF file by html string and add PageNumberStamp then save the file.
I’m sorry that it could be very basic question :slight_smile:


You can use following code snippet to add page number in the PDF Page after adding a HTML String:

Document doc = new Document();
Page page = doc.Pages.Add();
page.Paragraphs.Add(new HtmlFragment("Your HTML String"));
foreach(Page p in doc.Pages)
 TextStamp stamp = new TextStamp(p.Number + " of " + doc.Pages.Count);
  stamp.VerticalAlignment = VerticalAlignment.Bottom;
  stamp.HorizontalAlignment = HorizontalAlignment.Right;
doc.Save(dataDir + "output.pdf");
1 Like

thyank you very much @asad.ali .it is working perfactly :blush:

Please also tells me that there is any gereralize way by uising which we add PageNumberStamp without any loop in above case ?


The PageNumberStamp is also added on page level as you can notice in the code snippet:


So, if a PDF has more than one page, you have to iterate through each page in order to add it.