How to Flatten PDF Transparencies in .NET with C#

Learn how to use the Flatten Transparencies API Tool by pdfRest to flatten all transparencies in PDF files with C#
Share this page

Why Use Flatten Transparencies with C#?

The pdfRest Flatten Transparencies API Tool is a powerful resource for developers who need to process PDF files and ensure compatibility across various PDF viewers. This API tool allows you to flatten transparencies in a PDF, which can be crucial for printing purposes or when you need to finalize a document's visual elements. This tutorial will guide you through the process of sending an API call to Flatten Transparencies using C#.

Consider a scenario where a graphic designer has created a PDF with multiple layers and transparency effects. While the document looks great on screen, these effects can cause issues when printing, as not all printers can accurately reproduce them. By flattening the transparencies, the designer can convert these effects into a single layer, ensuring that the printed document closely matches what is seen on screen. This process is essential for professional printing and archiving purposes.

Flatten Transparencies 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, "flattened-transparencies-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");


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

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

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

Source of the provided code: GitHub

Breaking Down the Code

The above code snippet demonstrates how to make a multipart/form-data POST request to the pdfRest Flatten Transparencies API endpoint using C#. Let's break down the key components of this code:

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

This line initializes a new instance of the HttpClient class and sets the base address to the pdfRest API.

var request = new HttpRequestMessage(HttpMethod.Post, "flattened-transparencies-pdf");

Here we create a new HttpRequestMessage for a POST request to the 'flattened-transparencies-pdf' endpoint.

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

The API key is added to the request headers for authentication. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual pdfRest API key.

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

These lines read the PDF file as a byte array, create a ByteArrayContent object with it, and add it to the multipart/form-data content. Replace '/path/to/file.pdf' with the actual file path and 'file_name.pdf' with the desired name for the file.

byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

This sets the content type of the file part to 'application/pdf', indicating that the file is a PDF.

var response = await httpClient.SendAsync(request);

This line sends the request to the pdfRest API and awaits the response.

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

Finally, we read the response content as a string, which contains the API's result.

Beyond the Tutorial

By following the steps in this tutorial, you have learned how to use C# to call the pdfRest Flatten Transparencies API to process a PDF file. This can be a valuable tool in your programming toolkit, especially when dealing with documents that require a consistent visual output across different platforms.

To explore more capabilities and demo all of the pdfRest API Tools, visit the API Lab. For detailed information on the API, refer to the API Reference Guide.

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

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