Issue with Aspose.PDF C# Compression: Memory Overflow & Unexpected File Size Behavior

Background:
When using Aspose.PDF for C# to compress PDF files, I encounter two unexpected issues:

  1. Code 1 (Without Explicit Image Compression):

csharp

Document pdfDocument = new Document(dataDir + "input.pdf");  
pdfDocument.OptimizeResources();  
pdfDocument.Save(dataDir + "output.pdf");  
  • Result: A 20MB input file becomes ​30–40MB after compression.
  • Observation: Disabling OptimizeResources() resolves the size issue, but compression is not effective.
  1. Code 2 (With Explicit Image Compression):

csharp

Document CompressPdfDocument = new Document("document.pdf");  
OptimizationOptions PdfoptimizeOptions = new OptimizationOptions();  
PdfoptimizeOptions.ImageCompressionOptions.CompressImages = true;  
PdfoptimizeOptions.ImageCompressionOptions.ImageQuality = 50;  
CompressPdfDocument.OptimizeResources(PdfoptimizeOptions);  
CompressPdfDocument.Save("compressed.pdf");  
  • Result: Throws an ​out-of-memory error during OptimizeResources().
  • Workaround: Commenting out CompressImages = true prevents the error, but the output file is only ~5MB (smaller than expected).

Questions:

  1. Why does enabling CompressImages lead to memory overflow, while disabling it results in minimal compression?
  2. Why does the first code (OptimizeResources()) ​increase the file size instead of reducing it?
  3. How can I achieve significant PDF compression (e.g., 20MB → 5MB) without causing memory issues?

@bq1129
We are looking into it and will be sharing our feedback with you shortly.

@bq1129

If possible, can you please share your sample PDF document for our reference so that we can test the scenario in our environment and address it accordingly?