Aspose Cells .Net v. 22.3: Out of Memory Exception

Using version 22.3 and 22.4 of Aspose.Cell for .Net we are experiencing out of memory exceptions when trying to convert an excel file to pdf.

Using following code:
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.OnePagePerSheet = true;

using (var fileStream = new System.IO.FileStream(“C:\temp\Excel file that fails - empty.xlsx”, System.IO.FileMode.Open))
{
Workbook workbook = new Workbook(fileStream);
workbook.Save(“C:\temp\Excel file that fails - empty.pdf”, pdfSaveOptions);
}

Does not seem like I can upload the file that fails here. But I can send it if you contact me.

@sanjan007,

Please zip and attach the template Excel file here. We will check your issue soon.

Excel file that fails - empty.zip (35.5 KB)

@sanjan007,

I checked your file a bit. I opened into MS Excel and take the print preview of the first worksheet and found the first worksheet has more than 100K pages (mostly are blank with no data but formatting). So, you could think if you will try to render so many pages (also, you have set one page per sheet option on), the process would be huge. How could you render one page based on 100K pages, this will not be possible. So, when Aspose.Cells would try to render/merge so many pages, the process will surely demand more and more resources (memory, cpu, etc.) and consequently one will get out of memory error after sometime. I also spotted your data area is minimal, so you should set print area for each sheet accordingly. You may try the following sample code for your requirements, it works fine as I tested:
e.g.
Sample code:

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.OnePagePerSheet = true;
            

using (var fileStream = new System.IO.FileStream("e:\\test2\\Excel file that fails - empty.xlsx", System.IO.FileMode.Open))
{
       Workbook workbook = new Workbook(fileStream);
        foreach (var worksheet in workbook.Worksheets)
         {
              worksheet.PageSetup.PrintArea = worksheet.Cells.MaxDisplayRange.Address;
         }
workbook.Save("e:\\test2\\Excel file that fails - empty.pdf", pdfSaveOptions)
}

Hope, this helps a bit.

I see. Thanks.

Are there any good ways of aborting the Save operation after ie. 1 minute?

@sanjan007,

See the document with example code on how to stop conversion using InterruptMonitor when it is taking too long for your reference.

Tried using interrupt, but it does not seem to interrupt the save operation.

using Aspose.Cells; //22.03
using System.Threading;

namespace ExcelConvertToPdf
{
    internal class Converter
    {
        InterruptMonitor im = new InterruptMonitor();
        
        public void Start()
        {
            Thread t1 = new Thread(() =>
            {
                PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
                pdfSaveOptions.OnePagePerSheet = true;

                using (var fileStream = new System.IO.FileStream("C:\\temp\\Excel file that fails - empty.xlsx", System.IO.FileMode.Open))
                {
                    Workbook workbook = new Workbook(fileStream);
                    workbook.InterruptMonitor = im;

                    //var imgOptions = new ImageOrPrintOptions();
                    //var preview = new WorkbookPrintingPreview(workbook, imgOptions);
                    //if (preview.EvaluatedPageCount > 10000) throw new Exception("File is too large to be converted");

                    workbook.Save("C:\\temp\\Excel file that fails - empty.pdf", pdfSaveOptions);

                }
            });
            t1.Start();

            Thread t2 = new Thread(() =>
            {
                var duration = 0;

                while (true)
                {
                    if (!t1.IsAlive)
                        break;
                    if (duration > 5000)
                        break;

                    duration += 1000;
                    Thread.Sleep(1000);
                }

                im.Interrupt();
            });

            t2.Start();
            t1.Join();
            t2.Join();
        }
    }
}

@sanjan007,

Please notice, we found an issue with interrupting the Save process when using PdfSaveOptions.OnePagePerSheet option (on) for a large workbook. We have logged a ticket with an id “CELLSNET-50962” for your issue. We will look into it and figure it out soon.

Once we have an update on it, we will let you know.

Ok, thanks for your help. For now I guess I can limit size based on page count.

@sanjan007,

You are welcome. And, sure, for now you may evaluate page count attribute.

@sanjan007,

This is to inform you that your issue has been resolved now. The fix will be included in the next release (Aspose.Cells for .NET v22.5) which is scheduled in the second week of May 2022. You will also be notified when the next version is released.

The issues you have found earlier (filed as CELLSNET-50962) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi