"Not supported image type" exception when trying to compress a PDF

This exception occurs when trying to compress on a Windows host the following
output.pdf (15.7 KB)

with the option ImageCompressionOptions.ResizeImages = true
using the latest version (December 2024) of the Aspose PDF .NET nuget.

The XImage.FilterType is Flate…

@Olivier.G

The “Not supported image type” exception you are encountering when trying to compress a PDF using ImageCompressionOptions.ResizeImages = true may be related to the specific image formats present in your PDF. Aspose.PDF for .NET supports various image formats, but if the PDF contains images that are not supported or are in a format that cannot be processed, this exception can occur.

To troubleshoot this issue, consider the following steps:

  1. Check Image Formats: Ensure that the images in your PDF are in supported formats such as JPEG, PNG, or TIFF. If there are any unsupported formats, you may need to convert them to a supported format before compression.

  2. Use FlateDecode Compression: If your images are in a supported format, you can utilize FlateDecode compression for optimization. This can be done by setting the ImageFilterType to Flate when storing images in the XImageCollection. Here’s a code snippet to illustrate this:

    Document pdfDocument = new Document("input.pdf");
    OptimizationOptions optimizationOptions = new OptimizationOptions();
    optimizationOptions.ImageCompressionOptions.ResizeImages = true;
    optimizationOptions.ImageCompressionOptions.MaxResolution = 72; // Set your desired resolution
    optimizationOptions.ImageCompressionOptions.ImageQuality = 50; // Set your desired image quality
    
    pdfDocument.OptimizeResources(optimizationOptions);
    pdfDocument.Save("output.pdf");
    
  3. Update to the Latest Version: Ensure that you are using the latest version of Aspose.PDF, as updates may include fixes for issues related to image processing and compression.

  4. Review Documentation: For more detailed information on optimizing PDF documents and handling images, you can refer to the official Aspose documentation on optimizing PDF files.

If the issue persists after trying these steps, consider sharing the specific details of the images in your PDF or any additional error messages you receive for further assistance.

Sources:
[1]: Optimize, Compress or Reduce PDF Size in C#|Aspose.PDF for .NET

The file format is PPM.

Rather than raise an exception, do you have an option to simply skip the conversion of unsupported images files?

@Olivier.G

Can you please share the complete code sample that you are using? We tried with 24.12 version in our environment using below code snippet and could not face the exception:

Document document = new Document(dataDir + "output.pdf");
Optimization.OptimizationOptions pdfOptions = new Optimization.OptimizationOptions();
pdfOptions.CompressObjects = true;
pdfOptions.RemoveUnusedObjects = true;
pdfOptions.RemoveUnusedStreams = true;
pdfOptions.AllowReusePageContent = true;
pdfOptions.ImageCompressionOptions.ResizeImages = true;

document.OptimizeResources(pdfOptions);

document.Save(dataDir + "compressed.pdf");

compressed.pdf (15.5 KB)

@asad.ali

Here’s the code that I use

OptimizationOptions options = new()
{
    AllowReusePageContent = true,
    RemoveUnusedObjects = true,
    RemoveUnusedStreams = true,
    UnembedFonts = true,
    RemovePrivateInfo = true,
    LinkDuplicateStreams = true,
    CompressObjects = true
};

options.ImageCompressionOptions.CompressImages =  true;
options.ImageCompressionOptions.ImageQuality = 75;
options.ImageCompressionOptions.ResizeImages = true;
options.ImageCompressionOptions.MaxResolution = 300;

document.OptimizeResources(options);

document.OptimizeSize = true;
document.Optimize();

Please note that the exception is thrown by the method document.OptimizeResources.

After adding options one at a time, I found that the exception arises when

ImageCompressionOptions.CompressImages =  true

@Olivier.G

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-58955

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.

@asad.ali

Am I right to assume I’ll be notified in this forum if/when the issue is resolved?

Also, would you consider adding an option to skip images that could not be processed instead of failing with an exception?

@Olivier.G

Yes, your understandings are correct.

The ticket information has been updated accordingly and we will surely investigate from this perspective.