How to Convert PDF to TIFF in .NET with C#

Learn how to use pdfRest PDF to Images to convert PDF pages to TIFF images with C#
Share this page

Why Convert PDF to TIFF with C#?

The pdfRest PDF to Images API Tool offers a seamless way to convert PDF documents into image formats such as TIFF. This tutorial will guide you through the process of sending an API call to the PDF to Images endpoint using C#. By following this tutorial, you will learn how to interact with the pdfRest API and convert your PDF files into images programmatically.

Imagine you are working in a legal firm that needs to convert large volumes of PDF documents into TIFF images for archiving purposes. Using the PDF to Images API, you can automate this process, saving time and reducing the risk of errors associated with manual conversion. This can be particularly useful for ensuring that documents are stored in a consistent format that is suitable for long-term preservation.

PDF to TIFF 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, "tif"))
    {
        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");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name");
        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: GitHub

Breaking Down the Code

Let's break down the code to understand how it works:

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

This line initializes a new instance of HttpClient with the base address set to the pdfRest API endpoint.

using (var request = new HttpRequestMessage(HttpMethod.Post, "tif"))

A new HttpRequestMessage is created with the HTTP method set to POST and the endpoint set to "tif". This specifies that we are making a POST request to the PDF to Images API endpoint.

request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
request.Headers.Accept.Add(new("application/json"));

These lines add headers to the request. The Api-Key header is used for authentication, and the Accept header specifies that the client expects a JSON response.

var multipartContent = new MultipartFormDataContent();

A new instance of MultipartFormDataContent is created to hold the multipart form data content of the request.

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

These lines read the PDF file into a byte array and create a ByteArrayContent object from it. The file content is then added to the multipartContent with the name "file" and the filename "file_name". The Content-Type header is set to "application/pdf".

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

The multipartContent is set as the content of the request, and the request is sent asynchronously using httpClient.SendAsync.

var apiResult = await response.Content.ReadAsStringAsync();
Console.WriteLine("API response received.");
Console.WriteLine(apiResult);

The response content is read as a string and printed to the console.

Beyond the Tutorial

In this tutorial, you learned how to send an API call to the pdfRest PDF to Images endpoint using C#. This example demonstrated how to set up the request, add necessary headers, and handle the response.

To explore more functionalities of the pdfRest API, you can try out different API tools in the API Lab. For detailed information on all available endpoints and parameters, refer to the API Reference Guide.

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

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