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

Free Support Forum - aspose.com

OnBeforePageGenerate event not fired

Hi,

I am using Aspose.Pdf for .Net (19.6.0.0) and trying to use the OnBeforePageGenerate event but for some reason this event is not being called. Below is the code snippet which I have. Also I need the footer to be displayed as “Page 1 of 200”. I am not sure of the page number count until my pages are added. I tried to use doc.ProcessParagraphs() and doc.FreeMemory() but it takes almost 2-3 minutes for 150 page pdf.

var page = doc.Pages.Add();
page.OnBeforePageGenerate += (sender) => OnPageGenarate(sender);

private void OnPageGenarate(Page page)
{
PageNumberStamp pageNumberStamp = new PageNumberStamp();
pageNumberStamp.Background = false;
pageNumberStamp.Format = "Page # of " + page.Number;
pageNumberStamp.BottomMargin = 10;
pageNumberStamp.LeftMargin = 20;
pageNumberStamp.RightMargin = 10;
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Right;
pageNumberStamp.StartingNumber = 1;
// Set text properties
pageNumberStamp.TextState.Font = FontRepository.FindFont(“Arial”);
pageNumberStamp.TextState.FontSize = 8;
pageNumberStamp.TextState.FontStyle = FontStyles.Regular;
pageNumberStamp.TextState.ForegroundColor = Color.LightGray;
page.AddStamp(pageNumberStamp);

        // Header Text
        var headerText = new FormattedText($"Report 1",
           new FontColor(160, 160, 160),
           FontStyle.Helvetica,
           EncodingType.Winansi,
           false,
           8, 10f
       );

        headerText.AddNewLineText($"Printed: {DateTime.Now.ToString("MM/dd/yyyy")}");

        // Create header
        TextStamp textStamp = new TextStamp(headerText)
        {
            // Set properties of the stamp
            TopMargin = 15,
            LeftMargin = 25,
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,

        };

        page.AddStamp(textStamp);
    }

@kooldoode

A ticket with ID PDFNET-50650 has been created in our issue tracking system to further investigate the issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.