> 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/uwp/get-started/samples/optimizertest.md).

# Optimizer

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

Sample 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 [UWP SDK](/core/get-started/frameworks/dotnet.md).

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

```csharp
//
// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Foundation;

using pdftron.PDF;
using pdftron.SDF;

using PDFNetUniversalSamples.ViewModels;

namespace PDFNetSamples
{
    public sealed class OptimizerTest : Sample
    {
        public OptimizerTest() :
            base("Optimizer", "The sample shows how to use 'pdftron.PDF.Optimizer' to reduce PDF file size by reducing the file size, removing redundant information, and compressing data streams using the latest in image compression technology. 'pdftron.PDF.Optimizer' is an optional Add-On to PDFNet Core SDK.")
        {
        }

        public override IAsyncAction RunAsync()
        {
            return Task.Run(new System.Action(async () => {
                WriteLine("--------------------------------");
                WriteLine("Starting Optimizer Test...");
                WriteLine("--------------------------------\n");
                //--------------------------------------------------------------------------------
			    // Example 1) Simple optimization of a pdf with default settings. 
			    //
			    try
			    {
                    using (PDFDoc doc = new PDFDoc(Path.Combine(InputPath, "newsletter.pdf")))
                    {
                        doc.InitSecurityHandler();
                        Optimizer.Optimize(doc);
                        String output_file_path = Path.Combine(OutputPath, "newsletter_opt1.pdf");
                        WriteLine("Saving " + output_file_path);
                        await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
                        WriteLine("Done. Results saved in " + output_file_path);
                        await AddFileToOutputList(output_file_path).ConfigureAwait(false);
                    }
			    }
			    catch (Exception e)
			    {
				    WriteLine(GetExceptionMessage(e));
			    }
				
			    //--------------------------------------------------------------------------------
			    // Example 2) Reduce image quality and use jpeg compression for
			    // non monochrome images.
			    try
			    {
                    using (PDFDoc doc = new PDFDoc(Path.Combine(InputPath, "newsletter.pdf")))
                    {
                        doc.InitSecurityHandler();

                        OptimizerImageSettings image_settings = new OptimizerImageSettings();

                        // low quality jpeg compression
                        image_settings.SetCompressionMode(OptimizerImageSettingsCompressionMode.e_jpeg);
                        image_settings.SetQuality(1);

                        // Set the output dpi to be standard screen resolution
                        image_settings.SetImageDPI(144, 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);

                        // use the same settings for both color and grayscale images
                        OptimizerOptimizerSettings opt_settings = new OptimizerOptimizerSettings();
                        opt_settings.SetColorImageSettings(image_settings);
                        opt_settings.SetGrayscaleImageSettings(image_settings);


                        Optimizer.Optimize(doc, opt_settings);

                        String output_file_path = Path.Combine(OutputPath, "newsletter_opt2.pdf");
                        WriteLine("Saving " + output_file_path);
                        await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
                        WriteLine("Done. Results saved in " + output_file_path);
                        await AddFileToOutputList(output_file_path).ConfigureAwait(false);
                    }
			    }
			    catch (Exception e)
			    {
				    WriteLine(GetExceptionMessage(e));
			    }

			    //--------------------------------------------------------------------------------
			    // Example 3) Use monochrome image settings and default settings
			    // for color and grayscale images. 
			    try
			    {
                    using (PDFDoc doc = new PDFDoc(Path.Combine(InputPath, "newsletter.pdf")))
                    {
                        doc.InitSecurityHandler();

                        OptimizerMonoImageSettings mono_image_settings = new OptimizerMonoImageSettings();

                        mono_image_settings.SetCompressionMode(OptimizerMonoImageSettingsCompressionMode.e_jbig2);
                        mono_image_settings.ForceRecompression(true);

                        OptimizerOptimizerSettings opt_settings = new OptimizerOptimizerSettings();
                        opt_settings.SetMonoImageSettings(mono_image_settings);

                        Optimizer.Optimize(doc, opt_settings);

                        String output_file_path = Path.Combine(OutputPath, "newsletter_opt3.pdf");
                        WriteLine("Saving " + output_file_path);
                        await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
                        WriteLine("Done. Results saved in " + output_file_path);
                        await AddFileToOutputList(output_file_path).ConfigureAwait(false);
                    }
			    }
			    catch (Exception e)
			    {
				    WriteLine(GetExceptionMessage(e));
                }

                WriteLine("\n--------------------------------");
                WriteLine("Done Optimizer Test.");
                WriteLine("--------------------------------\n");
            })).AsAsyncAction();
		}
	}
}
```

{% 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/uwp/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.
