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

Learn how to convert TIFF images into PDF documents for your .NET projects by calling the pdfRest Convert to PDF API Tool.
Share this page

Why Convert TIFF to PDF with C#?

The pdfRest Convert to PDF API Tool is a powerful resource that allows developers to programmatically convert various file formats into PDFs. This tutorial will guide you through the process of sending an API call to convert a TIFF file to PDF using C#.

Imagine you have an application that generates images in TIFF format, and you need to distribute these images as PDFs for ease of printing and sharing. Using the Convert to PDF API, you can automate this conversion process within your application, enhancing the user experience and streamlining image and document management.

Convert TIFF to 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, "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");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "image/tiff");

        var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("output.pdf"));
        multipartContent.Add(byteArrayOption, "output");
        request.Content = multipartContent;
        var response = await httpClient.SendAsync(request);

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

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

Source: pdfRest API Samples on GitHub

Breaking Down the Code

In this code snippet, we create an HTTP client and send a multipart/form-data request to the Convert to PDF API endpoint:

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

We set the API key in the request headers to authenticate the request:

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

The file to be converted is read as a byte array and added to the multipart content, along with the content type of the original file:

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

Additional conversion options, such as page size and output file name, are also included in the request:

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

var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("output.pdf"));
multipartContent.Add(byteArrayOption2, "output");

Finally, the request is sent, and the response is read:

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

Beyond the Tutorial

By following the steps above, you have successfully called the Convert to PDF API using C#. This enables you to integrate PDF conversion functionality directly into your applications, providing a seamless experience for your users.

To explore all of the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/, and for more detailed information, refer to the API Reference documentation at https://pdfrest.com/documentation/.

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

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