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

Learn how to use pdfRest PDF to Images API Tool to convert PDF documents to JPG image files with C#
Share this page

Why Convert PDF to JPG with C#?

The pdfRest PDF to Images API Tool allows developers to convert PDF documents into image files programmatically. This API is useful in scenarios where you need to display PDF content as images on platforms that do not support PDF rendering natively, or when you want to generate thumbnails or previews of PDF documents for a user interface.

For example, an application that manages digital assets might use this API to create image previews of uploaded PDF files so that users can quickly browse through a collection without opening each file.

Convert PDF to JPG with C# Code Example

Here is the complete C# code example that demonstrates how to make an API call to the PDF to Images endpoint using pdfRest:

using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, "jpg"))
    {
        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: pdfRest API Samples on GitHub

Breaking Down the Code

This code snippet is a C# example of how to send a multipart/form-data request to the pdfRest API to convert a PDF file to images:

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

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

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

A new HttpRequestMessage is created with the HTTP method POST to the "jpg" endpoint, which is used to convert PDF to JPG images.

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

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

var multipartContent = new MultipartFormDataContent();

A new instance of MultipartFormDataContent is created to hold the parts of the multipart/form-data request.

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");

These lines read the PDF file as a byte array, create a ByteArrayContent with this array, and add it to the multipartContent with the name "file" and a filename. The content type for this part is set to "application/pdf".

request.Content = multipartContent;

The multipart content is then assigned to the Content property of the request.

var response = await httpClient.SendAsync(request);

This line sends the request asynchronously and waits for the response from the pdfRest API.

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

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

Beyond the Tutorial

In this tutorial, we walked through the process of making an API call to the pdfRest PDF to Images endpoint using C#. The code example provided demonstrates how to send a PDF file and receive the converted images in response. You can now explore and demo all of the pdfRest API Tools in the API Lab, and refer to the API Reference documentation for more details on the available endpoints and their usage.

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