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

Learn how to use .NET to convert BMP image files to PDF files by calling Convert to PDF API Tool by pdfRest.
Share this page

Why Use Convert to PDF with C#?

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

A real-world example of why a user might use Convert to PDF could be an enterprise application that generates reports in various formats and needs to standardize them into PDFs for archiving, sharing, or printing.

Convert 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.bmp");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "text/html");

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

Reference: pdfRest API C# Sample Code

Breaking Down the Code

The code above demonstrates how to make a multipart/form-data POST request to the pdfRest API to convert a document to PDF.

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

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

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

The API key is added to the request headers. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual API key.

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

Here, the file to be converted is read into a byte array and added to the multipart form data content with the name "file". Replace "/path/to/file" with the path to your source file, and "file_name" with the actual file name.

byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "text/html");

The content type for the file is set to "text/html". This should be changed to match the content type of your source file.

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

Finally, the request is sent asynchronously, and the response is read as a string. The result is then printed to the console.

Beyond the Tutorial

In this tutorial, we've accomplished the task of converting a document to PDF using the pdfRest API and C#. The code sample provided demonstrates a multipart/form-data request, which is suitable for file uploads. We encourage you to demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and to refer to the API Reference documentation at https://pdfrest.com/documentation/ for further exploration and understanding of the full capabilities of the API.

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

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