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

Learn how to use pdfRest PDF to Images API Tool to convert PDFs to PNG images with C#
Share this page

Why Convert PDF to PNG with C#?

The pdfRest PDF to Images API Tool is a powerful resource for converting PDF documents into image formats such as PNG. 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 use C# to interact with the pdfRest API and convert PDF files to images programmatically.

Imagine you are developing a document management system that needs to display PDF content as images within a web application. By converting PDF pages to images, you can easily render them in any browser without relying on PDF viewers. This approach can also be useful for generating thumbnails for PDF files, which can enhance user experience by providing quick previews.

PDF to PNG 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, "png"))
    {
        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

The provided code makes use of the HttpClient class to send an HTTP POST request to the pdfRest API. 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 URL.

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

Here, an HttpRequestMessage object is created for a POST request to the "png" endpoint, which is used for converting PDFs to PNG images.

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

This line adds the API key to the request headers. Replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with your actual API key.

request.Headers.Accept.Add(new("application/json"));

The request is configured to accept a JSON response.

var multipartContent = new MultipartFormDataContent();

A MultipartFormDataContent object is created to hold the file data to be sent in 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");

The PDF file is read into a byte array and added to the multipart content with the appropriate content type header.

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

The multipart content is set as the request content, and the request is sent asynchronously.

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

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

Beyond the Tutorial

In this tutorial, you learned how to use C# to send an API call to the pdfRest PDF to Images endpoint and convert a PDF file to PNG images.

To explore more functionalities of the pdfRest API, visit 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 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