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

Learn how to use .NET to Convert JPG to PDF by calling Convert to PDF API Tool by pdfRest.
Share this page

Why Convert JPG to PDF with C#?

The pdfRest Convert to PDF API Tool is a powerful resource that allows developers to convert various file formats into PDF documents. This tutorial will guide you through making an API call to Convert to PDF using C#.

You might need to convert JPG images depicting invoices, reports, or user manuals into a standardized PDF format for easy sharing, archiving, or printing.

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

        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");
        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 C# Sample Code

Breaking Down the Code

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

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

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

Here, a new HttpRequestMessage is created for a POST request to the "pdf" endpoint.

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

The API key is added to the request headers. This key is essential for authentication and should be replaced with your actual pdfRest API key.

var multipartContent = new MultipartFormDataContent();

A new MultipartFormDataContent object is created to hold the parts of the multipart request.

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

The file to be converted is read into a byte array, wrapped in a ByteArrayContent, and added to the multipart content. The "Content-Type" header is set to the MIME type of the file being sent.

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

This snippet sets the page size option for the PDF conversion.

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

The desired output file name is specified here.

var response = await httpClient.SendAsync(request);

The request is sent asynchronously, and the response is awaited.

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

Finally, the response content is read as a string, which contains the API result.

Beyond the Tutorial

In this tutorial, we've covered how to make a multipart API call to the pdfRest Convert to PDF endpoint using C#. This allows you to convert files to PDF programmatically within your C# applications. To explore all the capabilities of the pdfRest API Tools, you can demo them in the API Lab. For further details on the API, refer to the documentation.

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

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