How to Convert a PDF to a Single-Page PNG File using C# .NET Framework

Cloudmersive
2 min readFeb 2, 2024

--

When we convert our PDFs to PNG files, we can either generate image arrays (with one PNG per page of the original PDF) or stack all our PDF images into a single PNG file (creating a “tall” image). While both operations are useful, the latter process is a little more efficient, resulting in a compact single-page document image.

Using the code provided in this article, we can take advantage of a free API that converts our PDF documents to single-page PNG images, exactly as described above. Our resulting image file will contain a vertically stacked/concatenated array of images which we can conveniently review and share with relevant audiences.

To make our conversion, let’s start by installing the SDK. To install via NuGet, we can run the following command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2

Next, let’s prepare to authorize our API calls. We’ll need a free API key, which will allow us to make up to 800 API calls per month with no additional commitments (once we reach our total, it’ll reset the following month).

Finally, let’s go ahead and use the below code to call the function. We can load in our file for a multipart/form-data request, and we can copy our API key into the authorization snippet:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;

namespace Example
{
public class ConvertDocumentPdfToPngSingleExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertDocumentApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.

try
{
// Convert PDF to Single PNG image
byte[] result = apiInstance.ConvertDocumentPdfToPngSingle(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentPdfToPngSingle: " + e.Message );
}
}
}
}

Just like that, we have a quick & easy way to create single-page images of multi-page PDF documents.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet