How to Convert PDF to JPG/JPEG in C# .NET Framework
By converting our PDF documents to JPG files, we can make important content load faster on the web. Performing that conversion in C#/.NET requires some extra legwork, however, so a low-code API solution can make a big difference.
Using the below code, we can easily take advantage of a free PDF to JPG conversion API for our C#/.NET applications. This will allow us to convert multi-page PDF documents to lightweight JPG image arrays with full control over our output JPG quality.
Let’s start by installing the .NET SDK. We can do that by running the following command:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
Next, let’s get ready to authorize our API call by obtaining a free-tier API key. These allow a limit of 800 API calls per month with no commitment (once we reach our call limit, our total will reset in the following month).
With our API key ready, let’s copy the below code examples into our file. We can now customize our request parameters and call the function.
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 ConvertDocumentPdfToJpgExample
{
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.
var quality = 56; // int? | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75. (optional)
try
{
// Convert PDF to JPG/JPEG image array
PdfToJpgResult result = apiInstance.ConvertDocumentPdfToJpg(inputFile, quality);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentPdfToJpg: " + e.Message );
}
}
}
}
That’s all there is to it — now we can easily convert our PDFs to JPG arrays with minimal overhead.