7 ways to compress PDF files in C#, VB.NET
Tags: csharp, productivity, tutorial, dotnet
Published: 2019-07-31 11:00:46

---
title: 7 ways to compress PDF files in C#, VB.NET
published: true
description: This blog provides the best 7 possibilities to compress a PDF document based on reducing the size of the image content, optimizing font and so on. 
tags: csharp, productivity, tutorial, dotnet
canonical_url: https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx
---
Syncfusion [Essential PDF](https://www.syncfusion.com/products/file-formats/pdf/) is a .NET PDF library that can be used to optimize or compress your PDF documents. Reducing the PDF file size can help you by optimizing bandwidth cost, network transmission, and digital storage. It is especially useful in areas like archiving, emailing, and using PDF documents in web-based applications. Essential PDF supports the following optimizations:

* Shrinking all images.
* Optimizing fonts.
* Removing metadata.
* Optimizing page content.
* Disabling incremental updates.
* Removing or flattening form fields.
* Removing or flattening annotations.

In this blog, we will look at each of these optimizations and how to implement them.

##Reducing PDF file size by shrinking all images

PDF files may contain many images. Removing the images from a PDF file is usually not an option since they are necessary for many PDFs. Downsampling the images will decrease the number of pixels and is possibly the most effective way to reduce PDF file size. The user can control the PDF file size with respect to the quality of the image.
```csharp
//Create a new compression option object.
PdfCompressionOptions options = new PdfCompressionOptions();
options.CompressImages = true;
//Image quality is a percentage.
options.ImageQuality = 10;
```

##Reducing PDF file size by optimizing fonts

Not all the glyphs data and font tables are required to render the text in the PDF document. Remove the unused glyphs and unwanted font tables from the embedded fonts in a PDF document to make it smaller.
```csharp
PdfCompressionOptions options = new PdfCompressionOptions();
options.OptimizeFont = true;
```

##Reducing PDF file size by removing metadata

Sometimes unnecessary metadata might be present in the PDF file. Remove the unnecessary metadata, to reduce the size of the PDF.
```csharp
PdfCompressionOptions options = new PdfCompressionOptions();
options.RemoveMetadata = true;
```

##Reducing PDF file size by optimizing page contents

Optimizing the page contents will remove unwanted commented lines, white spaces, convert end-of-line characters to spaces and compress all uncompressed contents.

```csharp
PdfCompressionOptions options = new PdfCompressionOptions();
options.OptimizePageContents = true;
```
##Reducing PDF file size by disabling incremental updates

Incremental update is an option available to update the PDF file incrementally without rewriting the entire file. Here, changes are appended to the end of the file, leaving its original contents intact. Disabling the incremental update will rewrite the entire file, which results in a smaller PDF.

```csharp
//Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"input.pdf");
//Restructure the complete document.
loadedDocument.FileStructure.IncrementalUpdate = false;
```

##Reducing PDF file size by removing form fields

Removing or flattening form fields can help reduce your file size. When form fields and their values are not necessary, they can be removed. When they are necessary but require no further editing, they can be flattened. Both of these actions will result in reduced file size.

```csharp
public void RemoveFormFields(PdfLoadedDocument ldoc, bool flatten)
{
    if (flatten)
    {
        ldoc.Form.Flatten = true;
    }
    else
    {
        int count = ldoc.Form.Fields.Count;
        for (int i = count - 1; i >= 0; i--)
        {
            ldoc.Form.Fields.RemoveAt(i);
        }
    }
}
```

##Reducing PDF file size by removing annotations

Removing or flattening annotations can help reduce your file size. Annotations and their contents can be removed when they are not needed. When they are necessary but do not require additional editing, they can be flattened. Both of these options will result in reduced file size.

```csharp
public void RemoveAnnotations(PdfLoadedDocument ldoc, bool flatten)
{
    foreach(PdfPageBase page in ldoc.Pages)
    {
        if (flatten)
        {
           page.Annotations.Flatten = true;
        }
        else
        {
            int count = page.Annotations.Count;
            for (int i = count - 1;i >= 0; i--)
            {
              page.Annotations.RemoveAt(i);
            }
        }
    }  
}
```
<img style="width: 391px;" src="https://blog.syncfusion.com/wp-content/uploads/2018/08/image-309.png" />
<figcaption>PDF compression options</figcaption>
A sample demonstrating the available compression options in Essential PDF can be downloaded here: [OptimizePDFSample.zip](https://www.syncfusion.com/downloads/support/directtrac/general/ze/OptimizePDFSample-1079315236).

##Wrapping up
As you can see, [Essential PDF](https://www.syncfusion.com/products/file-formats/pdf/) provides a variety of optimizing mechanisms for compressing a PDF document. Use them effectively to generate compressed documents for increased efficiency and productivity in a document management system. Take a moment to peruse the [documentation](https://help.syncfusion.com/file-formats/pdf/working-with-compression), where you’ll find other options and features, all with accompanying code examples.

If you are new to our PDF library, it is highly recommended that you follow our [Getting Started guide](https://help.syncfusion.com/file-formats/pdf/getting-started).

If you’re already a Syncfusion user, you can download the product setup [here](https://www.syncfusion.com/account/downloads/studio/licensed/16_1_0_24) else, you can download a free, 30-day trial [here](https://www.syncfusion.com/downloads/fileformats/verify).

Have any questions or require clarifications about these features? Please let us know in the comments below, you can also contact us through our [support forum](https://www.syncfusion.com/forums) or [Direct-Trac](https://www.syncfusion.com/support/directtrac/). We are happy to assist you!

If you liked this blog post, we think you’ll also like the following related blog posts:

[HTML to PDF Conversion Using ASP.NET Core in Linux Docker](https://blog.syncfusion.com/post/html-to-pdf-conversion-using-asp-net-core-in-linux-docker.aspx)
[Adding RTL Support to Your PDF in Xamarin](https://blog.syncfusion.com/post/adding-rtl-support-to-your-pdf-in-xamarin.aspx)
[Now your PDF documents are safer!](https://blog.syncfusion.com/post/now-your-pdf-documents-are-safer.aspx)
[Exporting the Data Grid to a specific PDF page in Xamarin.Forms](https://blog.syncfusion.com/post/exporting-the-data-grid-to-a-specific-pdf-page-in-xamarin-forms.aspx)
[Optical Character Recognition in PDF Using Tesseract Open-Source Engine](https://blog.syncfusion.com/post/optical-character-recognition-in-pdf-using-tesseract-open-source-engine.aspx)

The post [7 ways to compress PDF files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx
) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs).