System.OutOfMemoryException | Aspose.PDF for .NET C#

Hi Team,

I am adding header with Page Number and watermark image in PDF and I am getting below error.

  1. There are different types pages(A0, A1, A2, A3, A4) in the same PDF
  2. 93 Pages in this PDF (One PDF may contain 700 pages in future).

Add watermark image in PDF is working if i will use separately but Add header is not working separately and as well as with watermark image.

I think this error is in Add Header for mixed page size of PDF .

System.OutOfMemoryException
HResult=0x8007000E
Message=Exception of type ‘System.OutOfMemoryException’ was thrown.
Source=Aspose.PDF
OutOfMemoryException.PNG (89.2 KB)

Request to you help to resolve this issue, Thanks in advance.
CatchError- OutOfMemoryException.PNG (72.9 KB)

CodeSnippet.PNG (92.3 KB)

@vivekhvs

Would you please also share the sample source PDF file for our reference along with the sample code snippet in text format? We will test the scenario in our environment and address it accordingly.

Thanks for your response.
PFB pdf and code both and I edited pdf because as you know we can upload up to 5 MB. In real scenario PDF size will be up to 700+ MB, Page up to 900+ and one pdf file contain different types of page size (A0, A1,…A4)

MixPDFSize.pdf (4.5 MB)

Sample Code

public void AddPageNumBCOInHeaderAndBackgroundImage(string PDFCurrentFile, string WatermarkText, string WatermarkImagePath)
{
int PdfPageCount = 1; float PDFHeight; float PDFWidth; //Variables
Document pdfDocument = new Document(PDFCurrentFile); //Open PDF
PdfFileInfo info = new PdfFileInfo(PDFCurrentFile);//to Get PDF INFO
Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();// Create HeaderFooter Object
// Create a Text object for Approved with BCO Number for Header
Aspose.Pdf.Text.TextFragment txtApproved = new Aspose.Pdf.Text.TextFragment(" Building Consent " + WatermarkText + " Approved by Council");
txtApproved.TextState.Font = FontRepository.FindFont(“Arial”); txtApproved.TextState.ForegroundColor = Color.Black; txtApproved.TextState.FontStyle = FontStyles.Bold;
txtApproved.IsInLineParagraph = true;
// Create Image Stamp Object to watermark image
ImageStamp imageStamp = new ImageStamp(WatermarkImagePath);
imageStamp.Background = false; imageStamp.Opacity = 0.2;
imageStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; imageStamp.VerticalAlignment = VerticalAlignment.Center;
foreach (Page page in pdfDocument.Pages)
{
PDFHeight = info.GetPageHeight(PdfPageCount); PDFWidth = info.GetPageWidth(PdfPageCount);//Get PDF Size
page.Header = header; header.Margin.Top = 5; header.Margin.Left = 10;
// Create a Text object for Page Number
Aspose.Pdf.Text.TextFragment txtPgNum = new Aspose.Pdf.Text.TextFragment("Page $p of $P ");
txtPgNum.TextState.Font = FontRepository.FindFont(“Arial”); txtPgNum.TextState.ForegroundColor = Color.Black; txtPgNum.TextState.FontStyle = FontStyles.Bold;
txtPgNum.IsInLineParagraph = true;
//Check Page Type and Incresase Font and Image size
if (PDFWidth > 550 && PDFWidth < 900) //A4 Page
{
txtPgNum.TextState.FontSize = 11; txtApproved.TextState.FontSize = 11;
imageStamp.Height = 550; imageStamp.Width = 430;
}
else if (PDFWidth > 900 && PDFWidth < 1350) //A3 Page
{
header.Margin.Left = 15; txtPgNum.TextState.FontSize = 19; txtApproved.TextState.FontSize = 19;
imageStamp.Height = 700; imageStamp.Width = 500;
}
else if (PDFWidth > 1350 && PDFWidth < 1950) //A2 Page
{
header.Margin.Left = 50; txtPgNum.TextState.FontSize = 25; txtApproved.TextState.FontSize = 25;
imageStamp.Height = 950; imageStamp.Width = 700;
}
else if (PDFWidth > 1950 && PDFWidth < 3000) //A1 Page
{
header.Margin.Left = 100; txtPgNum.TextState.FontSize = 37; txtApproved.TextState.FontSize = 37;
imageStamp.Height = 1400; imageStamp.Width = 1100;
}
else if (PDFWidth > 3000) //A0 Page
{
header.Margin.Left = 250; txtPgNum.TextState.FontSize = 60; txtApproved.TextState.FontSize = 60;
imageStamp.Height = 2250; imageStamp.Width = 1950;
}
header.Paragraphs.Add(txtPgNum); header.Paragraphs.Add(txtApproved);// Add stamp in Header
pdfDocument.Pages[PdfPageCount].AddStamp(imageStamp);// Add image stamp for Background watermark
PdfPageCount = PdfPageCount + 1; //Increase PDF Page Number for Background watermark
}
pdfDocument.Save(PDFCurrentFile); // Save the Pdf file
}

@vivekhvs

We used your code snippet to process the PDF file that you have shared. We did not notice any OutOfMemoryException. However, the generated PDF document did not seem correct. Could you please check it and share an expected output PDF document that you want to generate?

waterandheader.pdf (4.6 MB)

PS: We tested the scenario with 21.3v of the API which is latest.

Hi Asad,

PFA requested PDF.

Mixed Size.pdf (23.3 MB)

Hi Asad,

I was using 21.1 but now I update 21.3 and test same code but getting same exception after long time run.

OutOfMemoryException2.PNG (148.9 KB)

@vivekhvs

We tested the scenario using the file which you have recently shared. We noticed that the memory consumption was too high that program became unresponsive and we had to terminate it forcefully. Therefore, we have logged an investigation ticket as PDFNET-49626 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

Have you tried 21.3v with this file at your end? Do you notice OOM with this file as well?

We apologize for the inconvenience.