Stackoverflow on document.OptimizeResources(options)

Hello Aspose,

I have a problem in production
When i optimise a specifique PDF, application crash with a stackoverflow on document.OptimizeResources(options);
2020-07-03 Offre services Visio-audio conférence & vote à distance.pdf (151.2 KB)

an exemple of my code :

public static void Main(string[] args)
{
Aspose.Pdf.License asposeLicence = new Aspose.Pdf.License();
asposeLicence.SetLicense(“Aspose.Total.lic”);

string filename;
if (args == null || args.Length == 0)
{
throw new ArgumentNullException(“file is missing”);
}
else
{
filename = args[0];
}

FileInfo fichier = new FileInfo(filename);
if (!fichier.Exists)
{
Console.WriteLine($“file {fichier.FullName} not found”);
}

Console.WriteLine($“Test file {fichier.FullName}”);
using (Document document = new Document(fichier.FullName))
{
OptimisationPdf(document, true);
document.Save(fichier.FullName);
}

Console.WriteLine();
Console.WriteLine(“End”);
}

private static void OptimisationPdf(Document document, bool pageContent)
{
if (document is null)
{
return;
}

var options = new OptimizationOptions
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
AllowReusePageContent = pageContent,
UnembedFonts = true,
};

try
{
document.OptimizeResources(options);
}
catch (Exception ex)
{
Console.WriteLine(“optimisation exception”, ex);
}
}

@admin.grenoble

The issue you mentioned was reproducible at our side while testing the scenario with Aspose.PDF for .NET 21.1. Therefore, it has been logged as PDFNET-49264 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.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-49264) have been fixed in Aspose.PDF for .NET 21.8.

This issue seems to be back in version 23.7.

@iclemartin

Can you please share your sample PDF along with the sample code snippet? We will test the scenario in our environment and address it accordingly.

My PDF has information that I’d rather not share publicly. Is there another way to provide the PDF?

Here is the code that exhibits the stack overflow:

 public static byte[] ShrinkPdf(byte[] doc, string filename)
    {
        AsposeLicense.Init();
        var result = doc;

        try
        {
            using var stream = new MemoryStream(doc);
            using var document = new Document(stream);
            using var streamOut = new MemoryStream();

            document.Flatten();

            var optimizeOptions = new OptimizationOptions
            {
                AllowReusePageContent = true,
                CompressObjects = true,
                ImageCompressionOptions =
                {
                    CompressImages = true,
                    ImageQuality = 50,
                    Version = ImageCompressionVersion.Standard,
                },
                LinkDuplcateStreams = true,
                RemovePrivateInfo = true,
                RemoveUnusedObjects = true,
                RemoveUnusedStreams = true,
                UnembedFonts = true,
            };

            document.OptimizeResources(optimizeOptions);

            document.Save(streamOut, SaveFormat.Pdf);
            result = streamOut.ToArray();
        }
        catch (Exception ex)
        {
            _logger.LogError($"Unable to compress '{filename}'.", ex);
        }

        if (result.Length < doc.Length)
        {
            _logger.LogDebug($"{filename} compressed from {doc.Length} to {result.Length}");
            return result;
        }

        _logger.LogWarn($"{filename} compression failed; using uncompressed pdf; uncompressed size: {doc.Length} compressed size: {result.Length}");
        return doc;
    }

If I set AllowReusePageContent to false it compresses successfully.

@iclemartin

We have sent you a private message and you can please share your file there. We will test the scenario in our environment and address it accordingly.

File uploaded

@admin.grenoble

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-55328

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

We have paid support. Is there another place we should be requesting?

@iclemartin

You can create a post in paid support forum with a reference to the ticket ID logged here and it will be escalated to the highest priority.