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

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

Why Convert PDF to BMP with C#?

The pdfRest PDF to Images API Tool is a powerful resource for developers who need to convert PDF documents into image files programmatically. This tutorial will guide you through the process of sending an API call to the PDF to Images service using C#.

As an example, this API could be used for an application that processes documents and needs to display previews as images, or an archival system that converts stored PDFs into images for easier access and viewing.

Convert PDF to BMP 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, "bmp"))
    {
        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 Code

The code block above demonstrates how to perform a multipart/form-data POST request to the pdfRest API's BMP endpoint using C#. Let's break it down:

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 requests.

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

A new HttpRequestMessage is created for a POST request to the "bmp" endpoint, which is used for converting PDF to BMP images.

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

This line adds your API key to the request headers for authentication purposes.

var multipartContent = new MultipartFormDataContent();

A new instance of MultipartFormDataContent is created to hold the PDF file content.

var byteArray = File.ReadAllBytes("/path/to/file.pdf");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name.pdf");

The PDF file is read into a byte array, wrapped in a ByteArrayContent object, and added to the multipart content with the name "file".

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

This sets the content type of the file part to "application/pdf", indicating that the file being sent is a PDF.

var response = await httpClient.SendAsync(request);

The request is sent asynchronously and the response is captured in the "response" variable.

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

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

Beyond the Tutorial

In this tutorial, we've learned how to convert a PDF to BMP images using the pdfRest API and C#. By understanding the multipart/form-data request structure, you can adapt this example to convert PDFs into various image formats supported by pdfRest. 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