Memory Leak Issue in pdf.save(filename)

Hi there,

we are trying to create pdf file and attach One_Kb_image_file.jpg file into it. during the process we are using this code to get the width and height… if the file size is larger than 3KB everything works. but if it is less than it takes forever to save the file and start taking lot of memory eventually server get hung. which is this function (pdf.Save(pdfFileName); there is class file attached also attached with.

if we remove below code then it does take forever not mater how small file size is but i believe it will mess up stitching image correctly.

//if i remove this line the pdf.save(filename) does not get hung with any file size
using (var objImage = System.Drawing.Image.FromFile(file))
            {
                Logger.Instance.Info("Before Image width:" + objImage.Width);
                Logger.Instance.Info("Before Image height:" + objImage.Height);
                pdfImageSection.PageInfo.Width = objImage.Width;
                pdfImageSection.PageInfo.Height = objImage.Height;
            }

One_Kb_image_file.jpg (573 Bytes)

Here is the code :

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace WF.UAP.UA.EDelivery.UnitTest
{
[TestClass]
public class RetailConvertToPdf
{
    [TestMethod]
    public void TestJpegImage()
    {
        try
        {
            string imageFilePath = @"C:\One_Kb_image_file.jpg";
            string pdfFileName = CreateEmptyPdf(imageFilePath);

            using (var pdf = new Aspose.Pdf.Document())
            {
                Logger.Instance.Debug("Start stitching image files using aspose.");

                try
                {
                    StitchImages(pdf, imageFilePath);
                    pdf.Save(pdfFileName);
                    if (pdf.Pages != null)
                        pdf.Pages.FreeMemory();
                }
                catch (Exception ex)
                {
                    Logger.Instance.Error(ex.Message);
                }

            }
        }
        catch (Exception ex)
        {
            Assert.Fail(ex.Message);
        }
    }

    static string CreateEmptyPdf(string fileName)
    {
        try
        {
            fileName = Path.ChangeExtension(fileName, ".pdf");
            using (var doc = new Aspose.Pdf.Document())
            {
                doc.Save(fileName);
            }
        }
         
        catch (Exception ex)
        {
            throw;
        }
        return fileName;
    }
    private static void StitchImages(Aspose.Pdf.Document pdf, string file)
    {
        Aspose.Pdf.Image image = null;
        Logger.Instance.Debug("aspose stitching image for " + file);
        try
        {
            var pdfImageSection = pdf.Pages.Add();

            image = new Aspose.Pdf.Image
            {
                File = file
            };

            using (var objImage = System.Drawing.Image.FromFile(file)) //if we remove 
            {
                pdfImageSection.PageInfo.Width = objImage.Width;
                pdfImageSection.PageInfo.Height = objImage.Height;
            }

            pdfImageSection.Paragraphs.Add(image);
            // remove extra white space in four corners by setting page margin information
            pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 2f;

        }
        
        catch (Exception ex)
        {
            Logger.Instance.Error("Exception in StitchImages for " + file + "-" + ex.ToString());
            throw;
        }
        finally
        {
            if (image != null)
                image = null;
        }
    }
}
}

@andy.a.kuan.wellsfar

Thank you for contacting support.

We have worked with the data shared by you and have been able to reproduce the issue in our environment. A ticket with ID PDFNET-45895 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.