How to Convert a PNG Array to a PDF File using C#/.NET

Cloudmersive
2 min readJan 12, 2024

--

If we’re looking for a quick and easy way to convert our PNGs to PDF documents in our C#/.NET applications, we probably don’t want to be writing a bunch of code.

Thankfully, using the complementary code examples provided below, we can take advantage of a free API to make that conversion for us. We can use this API to create multi-page PDF documents out of 10+ PNG files at a time.

Our first step is to install the .NET SDK. Let’s run the below command in the Package Manager console to install via NuGet:

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

Next, let’s copy the below code examples into our file and configure our file content request parameters:

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 ConvertDocumentPngArrayToPdfExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertDocumentApi();
var inputFile1 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | First input file to perform the operation on.
var inputFile2 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Second input file to perform the operation on.
var inputFile3 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Third input file to perform the operation on. (optional)
var inputFile4 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Fourth input file to perform the operation on. (optional)
var inputFile5 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Fifth input file to perform the operation on. (optional)
var inputFile6 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Sixth input file to perform the operation on. (optional)
var inputFile7 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Seventh input file to perform the operation on. (optional)
var inputFile8 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Eighth input file to perform the operation on. (optional)
var inputFile9 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Ninth input file to perform the operation on. (optional)
var inputFile10 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Tenth input file to perform the operation on. (optional)

try
{
// Convert PNG Array to PDF
byte[] result = apiInstance.ConvertDocumentPngArrayToPdf(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentPngArrayToPdf: " + e.Message );
}
}
}
}

Finally, let’s grab a free-tier API key so we can authorize our requests at zero cost. Free-tier API keys allow a limit of 800 API calls per month with no additional commitment.

Just like that, we’re all done — no more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet