How to Linearize PDF in .NET with C#

Learn how to use C# to call the Linearize PDF API Tool from pdfRest to enable Fast Web View on your documents.
Share this page

Why Linearize PDF with C#?

The pdfRest Linearize PDF API Tool is a powerful resource that allows you to optimize PDFs for faster web viewing. Linearizing a PDF, also known as "web optimization" or "fast web view," restructures the file so that it can be read in a web browser before the entire file has been downloaded.

This tutorial will demonstrate how to send an API call to the Linearize PDF endpoint using C#. A real-world example of using Linearize PDF could be a web application that provides large PDFs, like e-books or product catalogues, and wants to improve user experience by allowing users to begin reading without waiting for the entire document to download.

Linearize 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, "linearized-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 code reference: pdfRest API Samples on GitHub.

Breaking Down the C# Code

The provided code is a C# example of how to make a POST request to the Linearize PDF endpoint of the pdfRest API:

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

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

using (var request = new HttpRequestMessage(HttpMethod.Post, "linearized-pdf"))

A new HttpRequestMessage is created with the HTTP method set to POST and the endpoint set to "linearized-pdf".

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

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

var multipartContent = new MultipartFormDataContent();

A MultipartFormDataContent object is created to hold the file content.

var byteArray = File.ReadAllBytes("/path/to/file.pdf");

The PDF file is read into a byte array from the specified path. Replace "/path/to/file.pdf" with the path to your PDF file.

multipartContent.Add(byteAryContent, "file", "file_name.pdf");

The byte array content is added to the multipart form data content with the name "file" and a file name "file_name.pdf". Replace "file_name.pdf" with the actual file name.

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

The content type of the byte array is set to "application/pdf" to indicate that the content is a PDF file.

var response = await httpClient.SendAsync(request);

The request is sent asynchronously and the response is awaited.

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

The response content is read as a string asynchronously, which contains the result of the API call.

Beyond the Tutorial

This tutorial walked you through the process of making an API call to the pdfRest Linearize PDF endpoint using C#. By executing this code, you have successfully sent a PDF file to be linearized, which can now be served more efficiently over the web. Feel free to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further exploration.

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.

Compare Plans
Contact Us