How to Reduce PDF File Size Generated with Aspose.PDF

Hello Aspose Team,

I’m reaching out regarding a significant discrepancy we’ve noticed in the PDF file sizes generated using Aspose.PDF.

Issue Summary

Despite generating identical content, PDFs created by our existing library are consistently smaller, while Aspose-generated PDFs tend to be larger in file size.

For example:

A very long document (~3,600 pages): Aspose → ~7.6 MB (compared to smaller expected size)

A mid-size document (~500 pages): Aspose → ~1.1 MB

Importantly, these documents do not contain large or high-resolution images, so image-heavy content is not the cause of the large file size.

Objective

We would like to reduce the file size of PDFs generated by Aspose.PDF without compromising document fidelity.

Could you please advise on:

  • Available optimization features (e.g., OptimizeResources, ImageCompressionOptions)?

  • Recommended parameter settings (image compression, font embedding, removal of unused objects)?

  • C# examples or best practices we can implement to minimize output size?

  • Any other tools or techniques (such as linearization or resource linking) to help reduce size?

We already attempted to optimize using this C# snippet:


 var optimizationOptions = new OptimizationOptions
{
  RemoveUnusedObjects = true,
  RemoveUnusedStreams = true,
  LinkDuplicateStreams = true,
  ImageCompressionOptions =
{
  CompressImages = true,
  ImageQuality = 0, 
  MaxResolution = 100
}
};

pdfDocument.OptimizeResources(optimizationOptions);
pdfDocument.Optimize();
pdfDocument.RemoveMetadata();

 pdfDocument.OptimizeResources(optimizationOptions);
 pdfDocument.Optimize();
 pdfDocument.RemoveMetadata();

Thank you for your guidance and support.

@Hilan2

To reduce the file size of PDFs generated with Aspose.PDF, you can utilize several optimization features and settings. Here’s a detailed approach to help you achieve this:

Cause

PDF files can become large due to unused objects, streams, and high-resolution images. Optimizing these elements can significantly reduce the file size.

Solution

You can implement the following optimization features and settings:

  1. OptimizeResources: This method helps in removing unused objects and streams, linking duplicate streams, and compressing images.
  2. ImageCompressionOptions: Set options for image compression, including quality and resolution.

Recommended Settings

  • RemoveUnusedObjects: Set to true to eliminate any objects that are not used in the document.
  • RemoveUnusedStreams: Set to true to remove any streams that are not referenced.
  • LinkDuplicateStreams: Set to true to link duplicate streams instead of storing them multiple times.
  • ImageCompressionOptions:
    • CompressImages: Set to true to enable image compression.
    • ImageQuality: Set to a lower value (e.g., 0 for maximum compression).
    • MaxResolution: Set to a lower resolution (e.g., 100).

C# Example

Here’s a refined version of your existing code snippet that incorporates these settings:

var optimizationOptions = new OptimizationOptions
{
    RemoveUnusedObjects = true,
    RemoveUnusedStreams = true,
    LinkDuplicateStreams = true,
    ImageCompressionOptions =
    {
        CompressImages = true,
        ImageQuality = 0, // Adjust for desired quality
        MaxResolution = 100 // Lower resolution for smaller size
    }
};

pdfDocument.OptimizeResources(optimizationOptions);
pdfDocument.RemoveMetadata(); // Remove any metadata to further reduce size

Additional Techniques

  • Linearization: This can help in optimizing the PDF for web viewing, which may also reduce file size.
  • Resource Linking: If you have repeated resources (like fonts or images), linking them can save space.

Next Steps

  1. Implement the above optimization settings in your code.
  2. Test the output file sizes and adjust the ImageQuality and MaxResolution parameters as needed.
  3. Consider using linearization if your PDFs are primarily for online viewing.

By following these steps, you should see a significant reduction in your PDF file sizes. If you have any further questions or need additional assistance, feel free to ask!