> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/ios/get-started/samples/optimizertest.md).

# Optimizer

This guide will walk you thorugh how to compress PDF file using Obj-C. WebViewer makes it easy to reduce images, Unembed fonts, Remove unused objects with Obj-C

Sample Obj-C code for using Apryse SDK to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology. Learn more about our [iOS SDK](/ios/guides.md).

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>

//---------------------------------------------------------------------------------------
// The following sample illustrates how to reduce PDF file size using 'pdftron.PDF.Optimizer'.
// The sample also shows how to simplify and optimize PDF documents for viewing on mobile devices 
// and on the Web using 'pdftron.PDF.Flattener'.
//
// @note Both 'Optimizer' and 'Flattener' are separately licensable add-on options to the core PDFNet license.
//
// ----
//
// 'pdftron.PDF.Optimizer' can be used to optimize PDF documents by reducing the file size, removing 
// redundant information, and compressing data streams using the latest in image compression technology. 
//
// PDF Optimizer can compress and shrink PDF file size with the following operations:
// - Remove duplicated fonts, images, ICC profiles, and any other data stream. 
// - Optionally convert high-quality or print-ready PDF files to small, efficient and web-ready PDF. 
// - Optionally down-sample large images to a given resolution. 
// - Optionally compress or recompress PDF images using JBIG2 and JPEG2000 compression formats. 
// - Compress uncompressed streams and remove unused PDF objects.
// ----
//
// 'pdftron.PDF.Flattener' can be used to speed-up PDF rendering on mobile devices and on the Web by 
// simplifying page content (e.g. flattening complex graphics into images) while maintaining vector text 
// whenever possible.
//
// Flattener can also be used to simplify process of writing custom converters from PDF to other formats. 
// In this case, Flattener can be used as first step in the conversion pipeline to reduce any PDF to a 
// very simple representation (e.g. vector text on top of a background image). 
//---------------------------------------------------------------------------------------
int main(int argc, char * argv[])
{
    @autoreleasepool {

        int ret = 0;

        // The first step in every application using PDFNet is to initialize the 
        // library and set the path to common PDF resources. The library is usually 
        // initialized only once, but calling Initialize() multiple times is also fine.
        [PTPDFNet Initialize: 0];

        //--------------------------------------------------------------------------------
        // Example 1) Simple optimization of a pdf with default settings. 
        //
        @try
        {
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [doc InitSecurityHandler];

            [PTOptimizer Optimize: doc settings: [[PTOptimizerSettings alloc] init]];

            [doc SaveToFile: @"../../TestFiles/Output/newsletter_opt1.pdf" flags: e_ptlinearized];
        }
        @catch (NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }

        //--------------------------------------------------------------------------------
        // Example 2) Reduce image quality and use jpeg compression for
        // non monochrome images.
        @try
        {
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [doc InitSecurityHandler];

            PTImageSettings *image_settings = [[PTImageSettings alloc] init];

            // low quality jpeg compression
            [image_settings SetCompressionMode: e_ptjpeg];
            [image_settings SetQuality: 1];

            // Set the output dpi to be standard screen resolution
            [image_settings SetImageDPI: 144 resampling: 96];

            // this option will recompress images not compressed with
            // jpeg compression and use the result if the new image
            // is smaller.
            [image_settings ForceRecompression: YES];

            // this option is not commonly used since it can 
            // potentially lead to larger files.  It should be enabled
            // only if the output compression specified should be applied
            // to every image of a given type regardless of the output image size
            //image_settings.ForceChanges(true);


            PTOptimizerSettings *opt_settings = [[PTOptimizerSettings alloc] init];
            [opt_settings SetColorImageSettings: image_settings];
            [opt_settings SetGrayscaleImageSettings: image_settings];

            // use the same settings for both color and grayscale images
            [PTOptimizer Optimize: doc settings: opt_settings];

            [doc SaveToFile: @"../../TestFiles/Output/newsletter_opt2.pdf" flags: e_ptlinearized];
        }
        @catch (NSException *e)
        {
            NSLog(@"%@", e.reason);    
            ret = 1;
        }
    

        //--------------------------------------------------------------------------------
        // Example 3) Use monochrome image settings and default settings
        // for color and grayscale images. 
        @try
        {
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [doc InitSecurityHandler];

            PTMonoImageSettings *mono_image_settings = [[PTMonoImageSettings alloc] init];

            [mono_image_settings SetCompressionMode: e_ptmn_jbig2];
            [mono_image_settings ForceRecompression: true];

            PTOptimizerSettings *opt_settings = [[PTOptimizerSettings alloc] init];
            [opt_settings SetMonoImageSettings: mono_image_settings];

            [PTOptimizer Optimize: doc settings: opt_settings];

            [doc SaveToFile: @"../../TestFiles/Output/newsletter_opt3.pdf" flags: e_ptlinearized];
        }
        @catch (NSException *e)
        {
            NSLog(@"%@", e.reason);    
            ret = 1;        
        }
        
        // ----------------------------------------------------------------------
        // Example 4) Use Flattener to simplify content in this document
        // using default settings
        @try
        {
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/TigerText.pdf"];
            [doc InitSecurityHandler];
            
            PTFlattener *fl = [[PTFlattener alloc] init];
            // The following lines can increase the resolution of background
            // images.
            //[fl SetDPI: 300];
            //[fl SetMaximumImagePixels: 5000000];

            // This line can be used to output Flate compressed background
            // images rather than DCTDecode compressed images which is the default
            //[fl SetPreferJPG: false];

            // In order to adjust thresholds for when text is Flattened
            // the following function can be used.
            //[fl SetThreshold: e_threshold_keep_most];

            // We use e_ptfast option here since it is usually preferable
            // to avoid Flattening simple pages in terms of size and 
            // rendering speed. If the desire is to simplify the 
            // document for processing such that it contains only text and
            // a background image e_simple should be used instead.
            [fl Process: doc mode: e_ptfast];
            [doc SaveToFile: @"../../TestFiles/Output/TigerText_flatten.pdf" flags: e_ptlinearized];
        }
        @catch (NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }

        // ----------------------------------------------------------------------
        // Example 5) Optimize a PDF for viewing using SaveViewerOptimized.
        @try
        {
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [doc InitSecurityHandler];
            
            PTViewerOptimizedOptions *options = [[PTViewerOptimizedOptions alloc] init];

            // set the maximum dimension (width or height) that thumbnails will have.
            [options SetThumbnailSize: 1500];

            // set thumbnail rendering threshold. A number from 0 (include all thumbnails) to 100 (include only the first thumbnail) 
            // representing the complexity at which SaveViewerOptimized would include the thumbnail. 
            // By default it only produces thumbnails on the first and complex pages. 
            // The following line will produce thumbnails on every page.
            // [options SetThumbnailRenderingThreshold: 0];

            [doc SaveViewerOptimized: @"../../TestFiles/Output/newsletter_SaveViewerOptimized.pdf" opts: options];
        }
        @catch (NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }

        [PTPDFNet Terminate: 0];        
        //NSLog(@"Done");
        return ret;
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

//---------------------------------------------------------------------------------------
// The following sample illustrates how to reduce PDF file size using 'pdftron.PDF.Optimizer'.
// The sample also shows how to simplify and optimize PDF documents for viewing on mobile devices
// and on the Web using 'pdftron.PDF.Flattener'.
//
// @note Both 'Optimizer' and 'Flattener' are separately licensable add-on options to the core PDFNet license.
//
// ----
//
// 'pdftron.PDF.Optimizer' can be used to optimize PDF documents by reducing the file size, removing
// redundant information, and compressing data streams using the latest in image compression technology.
//
// PDF Optimizer can compress and shrink PDF file size with the following operations:
// - Remove duplicated fonts, images, ICC profiles, and any other data stream.
// - Optionally convert high-quality or print-ready PDF files to small, efficient and web-ready PDF.
// - Optionally down-sample large images to a given resolution.
// - Optionally compress or recompress PDF images using JBIG2 and JPEG2000 compression formats.
// - Compress uncompressed streams and remove unused PDF objects.
// ----
//
// 'pdftron.PDF.Flattener' can be used to speed-up PDF rendering on mobile devices and on the Web by
// simplifying page content (e.g. flattening complex graphics into images) while maintaining vector text
// whenever possible.
//
// Flattener can also be used to simplify process of writing custom converters from PDF to other formats.
// In this case, Flattener can be used as first step in the conversion pipeline to reduce any PDF to a
// very simple representation (e.g. vector text on top of a background image).
//---------------------------------------------------------------------------------------

func runOptimizerTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0

        // The first step in every application using PDFNet is to initialize the
        // library and set the path to common PDF resources. The library is usually
        // initialized only once, but calling Initialize() multiple times is also fine.
        
        
        //--------------------------------------------------------------------------------
        // Example 1) Simple optimization of a pdf with default settings.
        //
        do {
            try PTPDFNet.catchException {
                let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                doc.initSecurityHandler()
                
                PTOptimizer.optimize(doc, settings: PTOptimizerSettings())
                
                doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_opt1.pdf").path, flags: e_ptlinearized.rawValue)
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        //--------------------------------------------------------------------------------
        // Example 2) Reduce image quality and use jpeg compression for
        // non monochrome images.
        do {
            try PTPDFNet.catchException {
                let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                doc.initSecurityHandler()
                
                let image_settings: PTImageSettings = PTImageSettings()
                
                // low quality jpeg compression
                image_settings.setCompressionMode(e_ptjpeg)
                image_settings.setQuality(1)
                
                // Set the output dpi to be standard screen resolution
                image_settings.setImageDPI(144, resampling: 96)
                
                // this option will recompress images not compressed with
                // jpeg compression and use the result if the new image
                // is smaller.
                image_settings.forceRecompression(true)
                
                // this option is not commonly used since it can
                // potentially lead to larger files.  It should be enabled
                // only if the output compression specified should be applied
                // to every image of a given type regardless of the output image size
                //image_settings.ForceChanges(true);
                
                let opt_settings: PTOptimizerSettings = PTOptimizerSettings()
                opt_settings.setColorImageSettings(image_settings)
                opt_settings.setGrayscaleImageSettings(image_settings)
                
                // use the same settings for both color and grayscale images
                PTOptimizer.optimize(doc, settings: opt_settings)
                
                doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_opt2.pdf").path, flags: e_ptlinearized.rawValue)
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        //--------------------------------------------------------------------------------
        // Example 3) Use monochrome image settings and default settings
        // for color and grayscale images.
        do {
            try PTPDFNet.catchException {
                let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                doc.initSecurityHandler()
                
                let mono_image_settings: PTMonoImageSettings = PTMonoImageSettings()
                
                mono_image_settings.setCompressionMode(e_ptmn_jbig2)
                mono_image_settings.forceRecompression(true)
                
                let opt_settings: PTOptimizerSettings = PTOptimizerSettings()
                opt_settings.setMonoImageSettings(mono_image_settings)
                
                PTOptimizer.optimize(doc, settings: opt_settings)
                
                doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_opt3.pdf").path, flags: e_ptlinearized.rawValue)
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        // ----------------------------------------------------------------------
        // Example 4) Use Flattener to simplify content in this document
        // using default settings
        do {
            try PTPDFNet.catchException {
                let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "TigerText", ofType: "pdf"))
                doc.initSecurityHandler()
                
                let fl: PTFlattener = PTFlattener()
                // The following lines can increase the resolution of background
                // images.
                //fl.setDPI(300)
                //fl.setMaximumImagePixels(5000000)
                
                // This line can be used to output Flate compressed background
                // images rather than DCTDecode compressed images which is the default
                //fl.setPreferJPG(false)
                
                // In order to adjust thresholds for when text is Flattened
                // the following function can be used.
                //fl.setThreshold(e_ptthreshold_keep_most)
                
                // We use e_ptfast option here since it is usually preferable
                // to avoid Flattening simple pages in terms of size and
                // rendering speed. If the desire is to simplify the
                // document for processing such that it contains only text and
                // a background image e_simple should be used instead.
                fl.process(doc, mode: e_ptfast)
                doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("TigerText_flatten.pdf").path, flags: e_ptlinearized.rawValue)
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        print("Done")
        return ret
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/ios/get-started/samples/optimizertest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
