Prerequisites

Before starting this tutorial, you should have:

  • An Aspose Cloud account with an active subscription
  • Your Client ID and Client Secret
  • A PowerPoint presentation with embedded fonts
  • Basic understanding of REST API concepts
  • Familiarity with font embedding in PowerPoint

Why Font Compression Matters

Embedding fonts in presentations ensures consistent rendering across different systems, but it can significantly increase file size. Font compression helps solve this problem by:

  1. Reducing file size by removing unused characters from embedded fonts
  2. Improving sharing efficiency with smaller file sizes for email and cloud storage
  3. Enhancing loading times when opening presentations
  4. Optimizing storage usage in your document management systems

Step 1: Compressing Embedded Fonts in a Presentation in Storage

Let’s learn how to compress embedded fonts in a presentation that’s stored in your cloud storage.

API Details

APITypeDescriptionResource
/slides/{name}/fonts/embedded/compressPOSTCompresses embedded fonts in a presentation.CompressEmbeddedFonts

Try It Yourself

Using cURL

First, authenticate to get your access token:

curl -v "https://api.aspose.cloud/connect/token" \
     -X POST \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -H "Accept: application/json"

Now, compress embedded fonts in your presentation:

curl -X POST "https://api.aspose.cloud/v3.0/slides/MyPresentation.pptx/fonts/embedded/compress" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

The API will compress all embedded fonts in the presentation and return a success status.

Step 2: Implementing in Different Programming Languages

Let’s see how to implement font compression in various programming languages:

C# Example

// Tutorial Code Example - Compressing Embedded Fonts in C#
using Aspose.Slides.Cloud.Sdk;
using Aspose.Slides.Cloud.Sdk.Api;
using System;

namespace AsposeSlidesCompressFonts
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create API instance with your credentials
            SlidesApi api = new SlidesApi("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
            
            // Name of the presentation in storage
            string presentationName = "MyPresentation.pptx";
            
            try
            {
                // First, let's check the presentation's size before compression
                var presentationInfo = api.GetSlidesDocument(presentationName);
                long sizeBeforeCompression = presentationInfo.SelfSize.Value;
                
                Console.WriteLine($"Presentation size before compression: {sizeBeforeCompression} bytes");
                
                // Compress embedded fonts in the presentation
                api.CompressEmbeddedFonts(presentationName);
                
                // Check the size after compression
                presentationInfo = api.GetSlidesDocument(presentationName);
                long sizeAfterCompression = presentationInfo.SelfSize.Value;
                
                Console.WriteLine($"Presentation size after compression: {sizeAfterCompression} bytes");
                Console.WriteLine($"Size reduction: {sizeBeforeCompression - sizeAfterCompression} bytes ({(1 - (double)sizeAfterCompression / sizeBeforeCompression) * 100:F2}%)");
                
                Console.WriteLine("Embedded fonts have been successfully compressed.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error compressing fonts: {ex.Message}");
            }
        }
    }
}

Python Example

# Tutorial Code Example - Compressing Embedded Fonts in Python
import asposeslidescloud
from asposeslidescloud.configuration import Configuration
from asposeslidescloud.apis.slides_api import SlidesApi

# Configure the API client
configuration = Configuration()
configuration.app_sid = 'YOUR_CLIENT_ID'
configuration.app_key = 'YOUR_CLIENT_SECRET'
api = SlidesApi(configuration)

# Name of the presentation in storage
presentation_name = "MyPresentation.pptx"

try:
    # First, let's check the presentation's size before compression
    presentation_info = api.get_slides_document(presentation_name)
    size_before_compression = presentation_info.self_size
    
    print(f"Presentation size before compression: {size_before_compression} bytes")
    
    # Compress embedded fonts in the presentation
    api.compress_embedded_fonts(presentation_name)
    
    # Check the size after compression
    presentation_info = api.get_slides_document(presentation_name)
    size_after_compression = presentation_info.self_size
    
    print(f"Presentation size after compression: {size_after_compression} bytes")
    size_reduction = size_before_compression - size_after_compression
    percentage = (1 - (size_after_compression / size_before_compression)) * 100
    print(f"Size reduction: {size_reduction} bytes ({percentage:.2f}%)")
    
    print("Embedded fonts have been successfully compressed.")
    
except Exception as ex:
    print(f"Error compressing fonts: {str(ex)}")

Java Example

// Tutorial Code Example - Compressing Embedded Fonts in Java
import com.aspose.slides.cloud.sdk.api.SlidesApi;
import com.aspose.slides.cloud.sdk.model.DocumentInfo;

public class CompressFontsExample {
    public static void main(String[] args) {
        try {
            // Create API instance with your credentials
            SlidesApi api = new SlidesApi("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
            
            // Name of the presentation in storage
            String presentationName = "MyPresentation.pptx";
            
            // First, let's check the presentation's size before compression
            DocumentInfo presentationInfo = api.getSlidesDocument(presentationName, null, null);
            long sizeBeforeCompression = presentationInfo.getSelfSize();
            
            System.out.println("Presentation size before compression: " + sizeBeforeCompression + " bytes");
            
            // Compress embedded fonts in the presentation
            api.compressEmbeddedFonts(presentationName, null, null, null);
            
            // Check the size after compression
            presentationInfo = api.getSlidesDocument(presentationName, null, null);
            long sizeAfterCompression = presentationInfo.getSelfSize();
            
            System.out.println("Presentation size after compression: " + sizeAfterCompression + " bytes");
            
            long sizeReduction = sizeBeforeCompression - sizeAfterCompression;
            double percentage = (1 - (double)sizeAfterCompression / sizeBeforeCompression) * 100;
            System.out.println("Size reduction: " + sizeReduction + " bytes (" + String.format("%.2f", percentage) + "%)");
            
            System.out.println("Embedded fonts have been successfully compressed.");
        } catch (Exception ex) {
            System.out.println("Error compressing fonts: " + ex.getMessage());
        }
    }
}

Step 3: Compressing Fonts in a Presentation in Request Body

Sometimes you need to compress fonts in a presentation that isn’t stored in the cloud. Aspose.Slides Cloud API provides an endpoint for this purpose:

API Details

APITypeDescriptionResource
/slides/fonts/embedded/compressPOSTCompresses embedded fonts in a presentation.CompressEmbeddedFontsOnline

C# Example for Online Mode

// Tutorial Code Example - Compressing Embedded Fonts Online in C#
using Aspose.Slides.Cloud.Sdk;
using Aspose.Slides.Cloud.Sdk.Api;
using System;
using System.IO;

namespace AsposeSlidesOnlineFontCompression
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create API instance with your credentials
            SlidesApi api = new SlidesApi("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
            
            // Local path to the presentation file
            string filePath = "MyPresentation.pptx";
            
            try
            {
                // Get the file size before compression
                FileInfo fileInfo = new FileInfo(filePath);
                long sizeBeforeCompression = fileInfo.Length;
                
                Console.WriteLine($"File size before compression: {sizeBeforeCompression} bytes");
                
                // Open the presentation file
                using (Stream file = File.OpenRead(filePath))
                {
                    // Compress embedded fonts and get the updated presentation
                    using (Stream updatedPresentation = api.CompressEmbeddedFontsOnline(file))
                    {
                        // Save the updated presentation
                        string outputPath = "Compressed_Presentation.pptx";
                        using (FileStream outputStream = File.Create(outputPath))
                        {
                            updatedPresentation.CopyTo(outputStream);
                        }
                        
                        // Get the file size after compression
                        FileInfo compressedFileInfo = new FileInfo(outputPath);
                        long sizeAfterCompression = compressedFileInfo.Length;
                        
                        Console.WriteLine($"File size after compression: {sizeAfterCompression} bytes");
                        Console.WriteLine($"Size reduction: {sizeBeforeCompression - sizeAfterCompression} bytes ({(1 - (double)sizeAfterCompression / sizeBeforeCompression) * 100:F2}%)");
                    }
                }
                
                Console.WriteLine("Embedded fonts have been successfully compressed and the presentation has been saved.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error compressing fonts: {ex.Message}");
            }
        }
    }
}

How Font Compression Works

When you compress embedded fonts in a presentation, Aspose.Slides Cloud performs the following optimizations:

  1. Character subset creation: It identifies which characters from the font are actually used in the presentation.
  2. Unused character removal: It removes all characters that aren’t used in the presentation.
  3. Font table optimization: It restructures the font data to minimize overhead.

This process maintains the visual quality of your text while significantly reducing the file size, especially for fonts with large character sets like CJK (Chinese, Japanese, Korean) fonts.

Best Practices for Font Compression

  1. Compress after all content is finalized: Apply compression when you’re done with content editing to ensure all necessary characters are included.

  2. Use selectively for presentations with embedded fonts: Font compression only helps when you have embedded fonts; it won’t affect presentations without them.

  3. Check rendering after compression: Occasionally, very complex fonts may have rendering issues after compression. Always verify the presentation appears correctly.

  4. Consider end-user needs: If recipients need to edit the text with the same font, they may need the full font. In this case, embedding without compression might be better.

Common Issues and Troubleshooting

No Significant Size Reduction

If you don’t see a significant file size reduction:

  1. Check if the presentation actually has embedded fonts
  2. The fonts might already be optimally embedded (only used characters)
  3. The embedded fonts might be small or already compressed

Text Rendering Issues

If text doesn’t render correctly after compression:

  1. The compression might have removed special glyphs or ligatures
  2. Try re-embedding the font without compression
  3. Consider using a different font with better compression characteristics

What You’ve Learned

In this tutorial, you’ve learned:

  • How to compress embedded fonts in a presentation to reduce file size
  • How to implement font compression for presentations in storage and in request bodies
  • The underlying mechanism of font compression
  • Best practices for using font compression effectively
  • Troubleshooting common issues with font compression

Further Practice

To reinforce your learning:

  1. Create a batch processing script to compress fonts in multiple presentations
  2. Compare file size reduction across different types of fonts (standard, decorative, CJK, etc.)
  3. Analyze the impact of font compression on loading times when opening presentations

Next Steps

Now that you know how to compress embedded fonts, learn how to replace presentation fonts when you need to substitute one font for another.

Helpful Resources