System.ArgumentException on save after optimize

Hello Aspose,

I have a problem in production
When i optimise a specifique PDF file an exception was throwed when i save after optimize.
L’intégral compta copro 2013.pdf (5.5 MB)
System.ArgumentException: An item with the same key has already been added. Key: 949
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at #=zRaSKdRnUDr0Fz00392Qu1B$q1vb6ZNXHFJ4gX28UOTSYHKwZit3IHPhxZsj1trvndQ==.#=z_4is2ec9$3x67SW3xckBQ03dVvYC()
at #=zRaSKdRnUDr0Fz00392Qu1B$q1vb6ZNXHFJ4gX28UOTSYHKwZit3IHPhxZsj1trvndQ==.#=zRwgRbAvPlRdYoXnVQQ==(#=zAcSDisdcGDzlWwMEtzx6WbR3L755F0AeWpuiMoI4EPoX #=zW0to8Ic2K$1f, Boolean #=zlst1nYPYIJHE)
at #=zRaSKdRnUDr0Fz00392Qu1B$q1vb6ZNXHFJ4gX28UOTSYHKwZit3IHPhxZsj1trvndQ==.#=zpND05s0=(#=zAcSDisdcGDzlWwMEtzx6WbR3L755F0AeWpuiMoI4EPoX #=zpyWslBnleD08, Boolean #=zlst1nYPYIJHE)
at #=zRaSKdRnUDr0Fz00392Qu1B$q1vb6ZNXHFJ4gX28UOTSYHKwZit3IHPhxZsj1trvndQ==.#=zeyDrmvU=(#=zAcSDisdcGDzlWwMEtzx6WbR3L755F0AeWpuiMoI4EPoX #=zW0to8Ic2K$1f, #=zqF5KpyV8WJCXEVZGGOMqC4Rtbew3OmYqUZQG4W8= #=zW0uH0Ig=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zfvhjY3Qu3xd36LWzbAUPaEOJb6d7vA78Kg==(Object #=zDDJvOck=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zRCNi2fqEWQ362rFSVrOP5eYF1r1e(MethodBase #=zDDJvOck=, Boolean #=zZeRwXB4=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zQyDZS9ls3a5rzlMeKg==(#=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M= #=zDDJvOck=, #=q_No3qr5DG9dnZJ19OprW6$bk4InS6Qbh2p3Uv_abcGY= #=zZeRwXB4=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zJO8HbDZogfMfusRp9rPPxXFJajh4hxjTp4ZKzjc=()
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zmFlh7IbhPkW7LWC_Jsu9XeA=(Boolean #=zDDJvOck=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zfvhjY3Qu3xd36LWzbAUPaEOJb6d7vA78Kg==(Object #=zDDJvOck=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zq$WzNViBNKrXX9WU_S0mI8caYKJv()
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zlxhQpp5H6DYYU4Kr4OQAS4Q=(Object #=zDDJvOck=, UInt32 #=zZeRwXB4=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zmFlh7IbhPkW7LWC_Jsu9XeA=(Boolean #=zDDJvOck=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zkbrogQeqOc_LPdFzGKIACMVgcBVB()
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zNapGncjtepYff64CVagr3Nzz5VOtCungWg==(Object[] #=zDDJvOck=, Type[] #=zZeRwXB4=, Type[] #=zip_JpN8=, Object[] #=z3jl8Ogk=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=z61KKcmdiSObAsoEpyZltB546VAqa(Stream #=zDDJvOck=, String #=zZeRwXB4=, Object[] #=zip_JpN8=, Type[] #=z3jl8Ogk=, Type[] #=zzmvmM$U=, Object[] #=zcB4cvVg=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zUwpFLg$kzT1LSzwbiA==(Stream #=zDDJvOck=, String #=zZeRwXB4=, Object[] #=zip_JpN8=)
at #=q3ySIAWqrQPUW3fwvmpn1VOOFNnp5n_60$W4_ycbt34M=.#=zmpPnGc5xeru2lcOlqwqbXrM=(Stream #=zDDJvOck=, String #=zZeRwXB4=, Object[] #=zip_JpN8=)
at #=zu5eBw$LK3$Wzlx6oianc0NeIMq52oVTGtg==.#=zeyDrmvU=()
at #=zAmc2vrhsN3uwtDkUDw1EN6wgTA6AaaeoCQ==.#=zeyDrmvU=(Stream #=zwVMH$hc=)
at #=zw8b1ppQkrTEFnvyjBO$_IT4q$54B.#=zeyDrmvU=(Stream #=zwVMH$hc=)
at Aspose.Pdf.Document.#=z9mRpoUfSiIRh(Stream #=zsO4PMg0=, SaveOptions #=zW0QpimwmGoJ5)
at Aspose.Pdf.Document.Save(Stream output)

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

We tested the scenario in our environment while using Aspose.PDF for .NET 21.1 and did not notice any exception. Please check following code snippet which was used for the testing and the attached output PDF generated by it:

Document doc = new Document(dataDir + @"L'intégral compta copro 2013.pdf");
var optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
optimizationOptions.RemoveUnusedObjects = true;
optimizationOptions.RemoveUnusedStreams = true;
optimizationOptions.AllowReusePageContent = true;
optimizationOptions.LinkDuplcateStreams = true;
optimizationOptions.UnembedFonts = true;
doc.OptimizeResources(optimizationOptions);
doc.Save(dataDir + @"ExampleOptimized_21.1.pdf");

ExampleOptimized_21.1.pdf (4.7 MB)

Would you please try using the latest version of the API and let us know if you still notice any issue.

Hello

In your code, you not launch OptimizeResources

best regards

@admin.grenoble

We apologize for the confusion.

We did use the OptimizeResources() method as you could notice that the shared output PDF had less size than the one which you provided. Furthermore, we have also updated the code snippet in our previous message.

try with this 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))
        {
            Console.WriteLine("save 1 to know filesize before optimisation");
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    document.Save(stream);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception Save MemoryStream\n{ex}");
            }

            OptimisationPdf(document, true);

            Console.WriteLine("save 2");
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    document.Save(stream);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception Save2 MemoryStream\n{ex}");
            }
        }

        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,
            SubsetFonts = false,
            UnembedFonts = true,
        };

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

@admin.grenoble

The issue seems related to usage of nested using statements where you are saving a Document object multiple times. Please note that Document.Save() method acts like disposing of an object as it frees the memory from allocated resources. Once you save a document, it is recommended to re-initialize it or create a new instance. You can change your code snippet as follows in order to avoid the error:

using (Document document = new Document(dataDir + @"L'intégral compta copro 2013.pdf"))
{
 Console.WriteLine("save 1 to know filesize before optimisation");
 try
 {
   using (MemoryStream stream = new MemoryStream())
   {
   document.Save(stream);
   stream.Position = 0;
   var doc = new Document(stream);
   OptimisationPdf(doc, true);

   Console.WriteLine("save 2");
   using (MemoryStream mstream = new MemoryStream())
   { 
    doc.Save(mstream);
   }
  }
 }
 catch (Exception ex)
 {
  Console.WriteLine($"Exception Save MemoryStream\n{ex}");
 }
}

How to re-initialize Pdf Document ?

@admin.grenoble

A new instance was created in the shared code snippet. We have also updated our previous response accordingly. We are sorry for the confusion.