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

Learn how to convert PNG images to PDF documents by calling the Convert to PDF API Tool in .NET.
Share this page

Why Use Convert to PDF with C#?

The pdfRest Convert to PDF API Tool is a powerful resource for developers looking to automate the conversion of images into PDF format. This tutorial will guide you through the process of sending an API call to Convert to PDF using C#.

A real-world example of using this API could be an application that generates images in various formats, like JPEG or PNG, and needs to convert them into PDFs for standardized distribution, viewer compatibility, or archival purposes.

Convert to PDF with C# Code Example

using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("") })
    using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf"))
        request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        var multipartContent = new MultipartFormDataContent();

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

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

Reference: pdfRest API C# code sample

Breaking Down the Code

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

  • HttpClient is initialized with the base address of the pdfRest API.
  • An HttpRequestMessage is configured for a POST request to the "pdf" endpoint.
  • The API key is added to the request headers, replacing "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual API key.
  • The content-type is set to accept JSON responses.
  • A MultipartFormDataContent object is created to hold the file and options.
  • The file to be converted is read into a byte array and added to the multipart content with the name "file" and a specified "file_name".
  • The content-type for the file is set to "text/html", indicating the type of the input file.
  • Additional options, such as "page_size" and "output" (the desired output file name), are added to the multipart content.
  • The multipart content is set as the request content, and the request is sent asynchronously.
  • The API response is read as a string and printed to the console.

Each parameter in the request is essential for the API to understand what is being sent and how to process it. The "page_size" and "output" are part of the conversion options you can specify. Refer to the pdfRest Cloud API Reference Guide for more details on the available options.

Beyond the Tutorial

By following the steps above, you've learned how to use C# to call the pdfRest Convert to PDF API. This allows you to integrate PDF conversion functionality into your applications seamlessly. You are encouraged to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further exploration of the API's capabilities.

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