Performance issue with PageNumberStamp

Hi,

I am trying to create page number stamp based on user input selected. Please see the code below.

string filePath = _dataDir + “sample.pdf”;

int PageRangeFrom = Convert.ToInt32(“1”);
int PageRangeTo = Convert.ToInt32(“41”);

Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(“Aspose.Pdf.lic”);
Aspose.Pdf.Document pdfDoc = null;
pdfDoc = new Aspose.Pdf.Document(filePath);

double HeaderMarginLeft = Convert.ToDouble(“1”);
double HeaderMarginRight = Convert.ToDouble(“1”);
double HeaderMarginTop = Convert.ToDouble(“0.5”);
string HeaderFontName = “Times-Roman”;
int HeaderFontSize = Convert.ToInt32(10");
string HeaderFontColor = “#000000”;
string HeaderLeft = “test”;
string HeaderCenter = “test”;
string HeaderRight = “test”;
double FooterMarginLeft = Convert.ToDouble(“1”);
double FooterMarginRight = Convert.ToDouble(“1”);
double FooterMarginBottom = Convert.ToDouble(“0.5”);
string FooterFontName = “Times-Roman”;
int FooterFontSize = Convert.ToInt32(“10”);
string FooterFontColor = “#000000”;
string FooterLeft = “test”;
string FooterCenter = “test”;
string FooterRight = “test”;
string NumberSettingFormat = “Numeric”.ToLower();
int NumberSettingStart = Convert.ToInt32(“1”);

//points to inches
HeaderMarginLeft = HeaderMarginLeft * 72;
HeaderMarginRight = HeaderMarginRight * 72;
HeaderMarginTop = HeaderMarginTop * 72;
FooterMarginLeft = FooterMarginLeft * 72;
FooterMarginRight = FooterMarginRight * 72;
FooterMarginBottom = FooterMarginBottom * 72;

PageNumberStamp htxtLStamp = null;
PageNumberStamp htxtCStamp = null;
PageNumberStamp htxtRStamp = null;
PageNumberStamp ftxtLStamp = null;
PageNumberStamp ftxtCStamp = null;
PageNumberStamp ftxtRStamp = null;

string headerLeftTxtReplaced = “”;
string headerCenterTxtReplaced = “”;
string headerRightTxtReplaced = “”;
string footerLeftTxtReplaced = “”;
string footerCenterTxtReplaced = “”;
string footerRightTxtReplaced = “”;

string hexHeader = HeaderFontColor;
System.Drawing.Color Hclr = System.Drawing.ColorTranslator.FromHtml(hexHeader);
string hexFooter = FooterFontColor;
System.Drawing.Color Fclr = System.Drawing.ColorTranslator.FromHtml(hexFooter);

int totalPageCount = pdfDoc.Pages.Count;

if (PageRangeFrom > 0 && PageRangeTo >= PageRangeFrom)
{
for (int i = PageRangeFrom; i <= PageRangeTo; i++)
{
Aspose.Pdf.Page page = pdfDoc.Pages[i];
if (NumberSettingStart <= totalPageCount)
{
string NumberSettingStartString = NumberSettingFormat == “roman” ? IntToRoman(NumberSettingStart) : NumberSettingStart.ToString();
string totalPageCountString = NumberSettingFormat == “roman” ? IntToRoman(totalPageCount) : totalPageCount.ToString();

         if (HeaderLeft.Trim() != "")
         {
             if (HeaderLeft == "<p>of<n>")
             {
                 headerLeftTxtReplaced = NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else if (HeaderLeft == "<p>")
             {
                 headerLeftTxtReplaced = NumberSettingStartString.ToString();
             }
             else if (HeaderLeft == "page<p>")
             {
                 headerLeftTxtReplaced = "Page" + " " + NumberSettingStartString;
             }
             else if (HeaderLeft == "page<p>of<n>")
             {
                 headerLeftTxtReplaced = "Page" + " " + NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else
             {
                 headerLeftTxtReplaced = HeaderLeft;
             }

             htxtLStamp = new PageNumberStamp(headerLeftTxtReplaced);
             htxtLStamp.NumberingStyle = NumberingStyle.NumeralsArabic;
             htxtLStamp.setStampId(101);
             htxtLStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(Hclr);
             htxtLStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(HeaderFontName);
             htxtLStamp.TextState.FontSize = float.Parse(HeaderFontSize.ToString());
             htxtLStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
             htxtLStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;
             htxtLStamp.TopMargin = HeaderMarginTop;
             htxtLStamp.LeftMargin = HeaderMarginLeft;
             htxtLStamp.RightMargin = HeaderMarginRight;
             page.AddStamp(htxtLStamp);
         }
         if (HeaderCenter.Trim() != "")
         {
             if (HeaderCenter == "<p>of<n>")
             {
                 headerCenterTxtReplaced = NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else if (HeaderCenter == "<p>")
             {
                 headerCenterTxtReplaced = NumberSettingStartString.ToString();
             }
             else if (HeaderCenter == "page<p>")
             {
                 headerCenterTxtReplaced = "Page" + " " + NumberSettingStartString;
             }
             else if (HeaderCenter == "page<p>of<n>")
             {
                 headerCenterTxtReplaced = "Page" + " " + NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else
             {
                 headerCenterTxtReplaced = HeaderCenter;
             }
             htxtCStamp = new PageNumberStamp(headerCenterTxtReplaced);
             htxtCStamp.setStampId(102);
             htxtCStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(Hclr);
             htxtCStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(HeaderFontName);
             htxtCStamp.TextState.FontSize = float.Parse(HeaderFontSize.ToString());
             htxtCStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
             htxtCStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;
             htxtCStamp.TopMargin = HeaderMarginTop;
             htxtCStamp.LeftMargin = HeaderMarginLeft;
             htxtCStamp.RightMargin = HeaderMarginRight;
             page.AddStamp(htxtCStamp);
         }

         if (HeaderRight.Trim() != "")
         {
             if (HeaderRight == "<p>of<n>")
             {
                 headerRightTxtReplaced = NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else if (HeaderRight == "<p>")
             {
                 headerRightTxtReplaced = NumberSettingStartString.ToString();
             }
             else if (HeaderRight == "page<p>")
             {
                 headerRightTxtReplaced = "Page" + " " + NumberSettingStartString;
             }
             else if (HeaderRight == "page<p>of<n>")
             {
                 headerRightTxtReplaced = "Page" + " " + NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else
             {
                 headerRightTxtReplaced = HeaderRight;
             }
             htxtRStamp = new PageNumberStamp(headerRightTxtReplaced);
             htxtRStamp.setStampId(103);
             htxtRStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(Hclr);
             htxtRStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(HeaderFontName);
             htxtRStamp.TextState.FontSize = float.Parse(HeaderFontSize.ToString());
             htxtRStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
             htxtRStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;
             htxtRStamp.TopMargin = HeaderMarginTop;
             htxtRStamp.LeftMargin = HeaderMarginLeft;
             htxtRStamp.RightMargin = HeaderMarginRight;
             page.AddStamp(htxtRStamp);
         }
         if (FooterLeft.Trim() != "")
         {
             if (FooterLeft == "<p>of<n>")
             {
                 footerLeftTxtReplaced = NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else if (FooterLeft == "<p>")
             {
                 footerLeftTxtReplaced = NumberSettingStartString.ToString();
             }
             else if (FooterLeft == "page<p>")
             {
                 footerLeftTxtReplaced = "Page" + " " + NumberSettingStartString;
             }
             else if (FooterLeft == "page<p>of<n>")
             {
                 footerLeftTxtReplaced = "Page" + " " + NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else
             {
                 footerLeftTxtReplaced = FooterLeft;
             }
             ftxtLStamp = new PageNumberStamp(footerLeftTxtReplaced);
             ftxtLStamp.setStampId(201);
             ftxtLStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(Fclr);
             ftxtLStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(FooterFontName);
             ftxtLStamp.TextState.FontSize = float.Parse(FooterFontSize.ToString());
             ftxtLStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
             ftxtLStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom;
             ftxtLStamp.BottomMargin = FooterMarginBottom;
             ftxtLStamp.LeftMargin = FooterMarginLeft;
             ftxtLStamp.RightMargin = FooterMarginRight;
             page.AddStamp(ftxtLStamp);
         }
         if (FooterCenter.Trim() != "")
         {
             if (FooterCenter == "<p>of<n>")
             {
                 footerCenterTxtReplaced = NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else if (FooterCenter == "<p>")
             {
                 footerCenterTxtReplaced = NumberSettingStartString.ToString();
             }
             else if (FooterCenter == "page<p>")
             {
                 footerCenterTxtReplaced = "Page" + " " + NumberSettingStartString;
             }
             else if (FooterCenter == "page<p>of<n>")
             {
                 footerCenterTxtReplaced = "Page" + " " + NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else
             {
                 footerCenterTxtReplaced = FooterCenter;
             }
             ftxtCStamp = new PageNumberStamp(footerCenterTxtReplaced);
             ftxtCStamp.setStampId(202);
             ftxtCStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(Fclr);
             ftxtCStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(FooterFontName);
             ftxtCStamp.TextState.FontSize = float.Parse(FooterFontSize.ToString());
             ftxtCStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
             ftxtCStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom;
             ftxtCStamp.BottomMargin = FooterMarginBottom;
             ftxtCStamp.LeftMargin = FooterMarginLeft;
             ftxtCStamp.RightMargin = FooterMarginRight;
             page.AddStamp(ftxtCStamp);
         }
         if (FooterRight.Trim() != "")
         {
             if (FooterRight == "<p>of<n>")
             {
                 footerRightTxtReplaced = NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else if (FooterRight == "<p>")
             {
                 footerRightTxtReplaced = NumberSettingStartString.ToString();
             }
             else if (FooterRight == "page<p>")
             {
                 footerRightTxtReplaced = "Page" + " " + NumberSettingStartString;
             }
             else if (FooterRight == "page<p>of<n>")
             {
                 footerRightTxtReplaced = "Page" + " " + NumberSettingStartString + " of" + " " + totalPageCountString;
             }
             else
             {
                 footerRightTxtReplaced = FooterRight;
             }
             ftxtRStamp = new PageNumberStamp(footerRightTxtReplaced);
             ftxtRStamp.setStampId(203);
             ftxtRStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(Fclr);
             ftxtRStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(FooterFontName);
             ftxtRStamp.TextState.FontSize = float.Parse(FooterFontSize.ToString());
             ftxtRStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
             ftxtRStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom;
             ftxtRStamp.BottomMargin = FooterMarginBottom;
             ftxtRStamp.LeftMargin = FooterMarginLeft;
             ftxtRStamp.RightMargin = FooterMarginRight;
             page.AddStamp(ftxtRStamp);
         }
         NumberSettingStart++;
     }
  }
}

}
pdfDoc.Save(_dataDir + “sample.pdf”);

when we tested the documents with pages 1000, 2k, 5k, 6k and 10k, it is running for more than 15mins. Kindly provide us a solution for this.

@prnksheela
Could you attach any document with which this is manifested.

FilesForTesting.7z (4.3 MB)

Please find the sample files and note that the values provided in the code is a sample. We are passing to the code according to the user input.

@prnksheela
Please provide the code for this method.
image.png (46.2 KB)

   private string IntToRoman(int num)
    {
        string romanResult = string.Empty;
        string[] romanLetters = {
    "M",
    "CM",
    "D",
    "CD",
    "C",
    "XC",
    "L",
    "XL",
    "X",
    "IX",
    "V",
    "IV",
    "I"
};
        int[] numbers = {
    1000,
    900,
    500,
    400,
    100,
    90,
    50,
    40,
    10,
    9,
    5,
    4,
    1
};
        int i = 0;
        while (num != 0)
        {
            if (num >= numbers[i])
            {
                num -= numbers[i];
                romanResult += romanLetters[i];
            }
            else
            {
                i++;
            }
        }
        return romanResult;
    }

This is the code we are using

@prnksheela
The code you attached runs in my environment (.Net 6, library version 23.8) for the file 10000Pages.pdf in 3.2 seconds. Do you think this is not a good enough result or is the execution time noticeably longer in your environment?

In our application, it is taking more than 15 mins (.Net 4.8, library version 22.8).

@prnksheela
In my environment for .Framework 4.8, library version 22.8 is processed in the same time - just over three seconds.
Is it possible for you to view the execution time on another computer?

I have done few corrections, could you please check this code?

@prnksheela
image.png (39.2 KB)
You can find out why operators like this are used:

double FooterMarginLeft = Convert.ToDouble(“1”);
double FooterMarginRight = Convert.ToDouble(“1”);
double FooterMarginBottom = Convert.ToDouble(“0.5”);

instead of:

double FooterMarginLeft = 1;
double FooterMarginRight = 1;
double FooterMarginBottom = 0.5;

?
When I paste the code you provided into the editor, it does not put quotes and I have to replace all of them.

@prnksheela
In this variant, my execution time is 44 seconds.