How to Compress PDF Files in .NET with C#

Learn how to use the Compress PDF API Tool from pdfRest to programmatically compress PDF files with C#.
Share this page

Why Compress PDF with C#?

The pdfRest Compress PDF API Tool is a powerful resource that allows developers to reduce the size of PDF files programmatically. This is particularly useful when dealing with large documents that need to be shared over email or stored in environments with limited space.

For instance, a business might need to compress a batch of reports to archive them efficiently or to ensure that they can be emailed to clients without hitting attachment size limits.

This tutorial will show you how to send an API call to Compress PDF with C# using the pdfRest API.

Compress PDF with C# Code Example

using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, "compressed-pdf"))
    {
        request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        request.Headers.Accept.Add(new("application/json"));
        var multipartContent = new MultipartFormDataContent();

        var byteArray = File.ReadAllBytes("/path/to/file.pdf");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name.pdf");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

        var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("high"));
        multipartContent.Add(byteArrayOption, "compression_level");

        request.Content = multipartContent;
        var response = await httpClient.SendAsync(request);

        var apiResult = await response.Content.ReadAsStringAsync();

        Console.WriteLine("API response received.");
        Console.WriteLine(apiResult);
    }
}

Source code reference: pdf-rest-api-samples

Breaking Down the C# Code

The code snippet above demonstrates how to make an API call to the pdfRest Compress PDF endpoint using C#. Let's break down the key parts of the code:

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })

This line initializes an HttpClient instance with the base address set to the pdfRest API.

request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

Here, an API key is added to the request headers for authentication. Replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with your actual API key.

var byteArray = File.ReadAllBytes("/path/to/file.pdf");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name.pdf");

This snippet reads a PDF file from the specified path and adds it to the multipart form data content with the key file.

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("high"));
multipartContent.Add(byteArrayOption, "compression_level");

The compression level is set to 'high' and added to the multipart content. The available compression levels can be found in the pdfRest Cloud API Reference Guide.

var response = await httpClient.SendAsync(request);

This line sends the API request and waits for the response.

var apiResult = await response.Content.ReadAsStringAsync();

Finally, the response from the API is read as a string and can be processed or saved as needed.

Beyond the Tutorial

In this tutorial, you've learned how to compress a PDF file using C# and the pdfRest Compress PDF API. By following the steps outlined above, you have made an API call to reduce the size of a PDF document. This can help you manage files more efficiently in your applications.

We encourage you to try all of the pdfRest API Tools in the API Lab at API Lab and refer to the API Reference documentation at Cloud API Reference Guide for further exploration.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.