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

Free Support Forum - aspose.com

How to pad number using PageNumberStamp

I am trying to add a page identifier to the bottom right-hand corner of every page in a given PDF. I am currently testing the PageNumberStamp object but am not getting the desired results. We keep a consistent format where the number is padded with leading 0. an example is “ABC 000001”. This works with the PageNumberStamp object until you get to say page 100 of the document where the stamp comes out like “ABC 00000100” instead of “ABC 000100”.
Is there a way to achieve this?

Below is a snippet of code I am testing:

var pdfDoc = new Document(filePath)
// Create page number stamp
var pageNumberStamp = new PageNumberStamp();

                var formattedNumber = "#".ToString(CultureInfo.InvariantCulture).PadLeft(6, '0');
                pageNumberStamp.Format = $"ABC {formattedNumber}";

                // Whether the stamp is background
                pageNumberStamp.Background = false;
                pageNumberStamp.TopMargin = 10;
                pageNumberStamp.BottomMargin = 1;
                pageNumberStamp.RightMargin = 5;
                pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Right;
                pageNumberStamp.StartingNumber = _stamp.BatesNumberStart;
                // Set text properties
                pageNumberStamp.TextState.Font = FontRepository.FindFont(StampFont);
                pageNumberStamp.TextState.FontSize = 14.0F;
                pageNumberStamp.TextState.ForegroundColor = Color.Black; 

                for (var pageIndex = 1; pageIndex <= pdfDoc.Pages.Count; pageIndex++)
                {
                    var page = pdfDoc.Pages[pageIndex];

                    Rectangle rec = page.GetPageRect(true);
                    double pageWidth = rec.Width;
                    double pageHeight = rec.Height;
                    page.SetPageSize(pageWidth, pageHeight + 15);
                    page.AddStamp(pageNumberStamp);
                }

                // Save output document
                pdfDoc.Save(tempFilePath);

@jack.montagna

Would you kindly share the sample PDF document for us. We will test the scenario in our environment and address it accordingly.