The PDF to Image Converter component allows you to convert PDF pages into PNG images. This component is distributed as part of the Winnovative.Pdf.Next.PdfProcessor.Windows NuGet package when targeting Windows and as part of the Winnovative.Pdf.Next.PdfProcessor.Linux package when targeting Linux. The package for Windows is referenced by the Winnovative.Pdf.Next.Windows meta package and the package for Linux is referenced by the Winnovative.Pdf.Next.Linux meta package.
You can set the output image's color space and resolution. You can also choose to make the background transparent and specify the page range in the PDF document to be converted. Conversion of password protected PDF documents is also supported and you can provide both user and owner passwords.
The Winnovative.Pdf.NextPdfToImageConverter class allows you to load a PDF file and convert its pages into PNG images.
The Winnovative.Pdf.NextPdfToImageConverter class is used to convert PDF documents to images. You can create an instance using the default constructor, which initializes the converter with standard settings. These settings can later be customized through properties like PdfToImageConverterColorSpace, PdfToImageConverterResolution, PdfToImageConverterTransparencyEnabled and others, which control the conversion process.
// Create a new PDF to Image converter instance
PdfToImageConverter pdfToImageConverter = new PdfToImageConverter();Note that PdfToImageConverter instances are not reusable. You must create a new instance for each conversion. Reusing an instance after a completed conversion will result in an exception.
The PdfToImageConverterColorSpace property of Winnovative.Pdf.NextPdfPageImageColorSpace type controls the color space of the resulting images. It can be set to PdfPageImageColorSpaceRGB to produce color images, to PdfPageImageColorSpaceMono to produce black and white images or to PdfPageImageColorSpaceGray to produce grayscale images. The default value is RGB, which means that the converter will produce color images.
pdfToImageConverter.ColorSpace = PdfPageImageColorSpace.RGB;The PdfToImageConverterResolution property controls the resolution in DPI of the resulting images. The default is 150 DPI.
pdfToImageConverter.Resolution = 150;The PdfToImageConverterTransparencyEnabled property controls whether background transparency is enabled for the images. It can be set to true only when the ColorSpace property is RGB or Gray. The default is false.
pdfToImageConverter.TransparencyEnabled = true;If the PDF document you convert to image is password protected you have to specify the user or owner password to be used to decrypt the PDF document before conversion. You can set the user password in the PdfToImageConverterUserPassword property and the owner password in the PdfToImageConverterOwnerPassword property.
pdfToImageConverter.UserPassword = userPasswordString;
pdfToImageConverter.OwnerPassword = ownerPasswordString;The PdfToImageConverterMaxPageCount property controls the upper limit for the number of PDF pages to process. The PDF page range to convert to images can be set in the conversion methods. The default is 0, which means there is no upper limit.
pdfToImageConverter.MaxPageCount = 0;pdfToImageConverter.MaxPageCount = 10;To convert all pages in a PDF document from a memory buffer to images, use the PdfToImageConverterConvertToImages(Byte) method. The parameter is the PDF document read into a memory buffer.
The function returns an array of Winnovative.Pdf.NextPdfPageImage objects. The PdfPageImage object contains information such as the page number in PdfPageImagePageNumber and the PDF page image data in PNG format in PdfPageImageImageData.
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfBytes);To convert PDF document pages from a memory buffer to images starting at a given page number through the end of the document, use the PdfToImageConverterConvertToImages(Byte, Int32) method. The first parameter is the PDF document read into a memory buffer and the second parameter is the 1-based start page number.
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfBytes, startPageNumber);To convert PDF document pages from a memory buffer to images starting at a given page number until the end page number inclusive, use the PdfToImageConverterConvertToImages(Byte, Int32, Int32) method. The first parameter is the PDF document read into a memory buffer and the second and third parameters are the 1-based start and end page numbers. If the end page number is 0, the conversion continues to the end of the document.
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfBytes, startPageNumber, endPageNumber);There are also similar methods to convert PDF pages to images that accept a PDF stream or a PDF file path.
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfStream);
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfFile);PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfStream, startPageNumber);
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfFile, startPageNumber);PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfStream, startPageNumber, endPageNumber);
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertToImages(inputPdfFile, startPageNumber, endPageNumber);The conversion methods above create the images in memory. There are similar methods to convert PDF pages to image files on disk in a specified folder. The parameters of these methods are the same as above with two additional parameters specifying the output folder path and the output file name without extension, which will be used as a base name for the generated image files. The final image file names will be formed by appending the page number to the base name. The output folder is created if it does not exist.
pdfToImageConverter.ConvertToImageFiles(inputPdfBytes, outputDirectory, imageFileName);
pdfToImageConverter.ConvertToImageFiles(inputPdfStream, outputDirectory, imageFileName);
pdfToImageConverter.ConvertToImageFiles(inputPdfFile, outputDirectory, imageFileName);pdfToImageConverter.ConvertToImageFiles(inputPdfBytes, startPageNumber, outputDirectory, imageFileName);
pdfToImageConverter.ConvertToImageFiles(inputPdfStream, startPageNumber, outputDirectory, imageFileName);
pdfToImageConverter.ConvertToImageFiles(inputPdfFile, startPageNumber, outputDirectory, imageFileName);pdfToImageConverter.ConvertToImageFiles(inputPdfBytes, startPageNumber, endPageNumber, outputDirectory, imageFileName);
pdfToImageConverter.ConvertToImageFiles(inputPdfStream, startPageNumber, endPageNumber, outputDirectory, imageFileName);
pdfToImageConverter.ConvertToImageFiles(inputPdfFile, startPageNumber, endPageNumber, outputDirectory, imageFileName);There are also asynchronous variants of these methods that follow the Task-based Asynchronous Pattern (TAP) in .NET, allowing PDF to image conversion to run in parallel using async and await. These methods share the same names as their synchronous counterparts and include the "Async" suffix. They also accept an optional System.ThreadingCancellationToken parameter that can be used to cancel the conversion operation where applicable.
To convert all pages in a PDF document from a memory buffer to images, use the PdfToImageConverterConvertToImagesAsync(Byte, CancellationToken) method. The parameter is the PDF document read into a memory buffer.
PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfBytes);To convert PDF document pages from a memory buffer to images starting at the given page number through the end of the document, use the PdfToImageConverterConvertToImagesAsync(Byte, Int32, CancellationToken) method. The first parameter is the PDF document read into a memory buffer, and the second parameter is the 1-based start page number.
PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfBytes, startPageNumber);To convert PDF document pages from a memory buffer to images starting at the given page number up to the end page number inclusive, use the PdfToImageConverterConvertToImagesAsync(Byte, Int32, Int32, CancellationToken) method. The first parameter is the PDF document read into a memory buffer, and the second and third parameters are the 1-based start and end page numbers. If the end page number is 0, the conversion continues to the end of the document.
PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfBytes, startPageNumber, endPageNumber);There are also similar methods to convert PDF pages to images that accept a PDF stream or a PDF file path.
PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfStream);
PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfFile);PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfStream, startPageNumber);
PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfFile, startPageNumber);PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfStream, startPageNumber, endPageNumber);
PdfPageImage[] pdfPageImages = await pdfToImageConverter.ConvertToImagesAsync(inputPdfFile, startPageNumber, endPageNumber);The conversion methods above create the images in memory. There are similar methods to convert PDF pages to image files on disk in a specified folder. The parameters of these methods are the same as above with two additional parameters specifying the output folder path and the output file name without extension, which will be used as a base name for the generated image files. The final image file names will be formed by appending the page number to the base name. The output folder is created if it does not exist.
await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfBytes, outputDirectory, imageFileName);
await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfStream, outputDirectory, imageFileName);
await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfFile, outputDirectory, imageFileName);await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfBytes, startPageNumber, outputDirectory, imageFileName);
await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfStream, startPageNumber, outputDirectory, imageFileName);
await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfFile, startPageNumber, outputDirectory, imageFileName);await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfBytes, startPageNumber, endPageNumber, outputDirectory, imageFileName);
await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfStream, startPageNumber, endPageNumber, outputDirectory, imageFileName);
await pdfToImageConverter.ConvertToImageFilesAsync(inputPdfFile, startPageNumber, endPageNumber, outputDirectory, imageFileName);The PdfToImageConverterConversionInfo property exposes an object of Winnovative.Pdf.NextPdfToImageConversionInfo type which is populated after the conversion completes successfully with information about the conversion process such as the number of pages converted.
int numberOfPagesConverted = pdfToImageConverter.ConversionInfo.PageCount;using System;
using System.IO;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Winnovative_Next_AspNetDemo.Models;
using Winnovative_Next_AspNetDemo.Models.PDF_to_Image;
// Use Winnovative Namespace
using Winnovative.Pdf.Next;
namespace Winnovative_Next_AspNetDemo.Controllers.PDF_to_Image
{
public class PDF_to_ImageController : Controller
{
private readonly IWebHostEnvironment m_hostingEnvironment;
public PDF_to_ImageController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
public IActionResult Index()
{
var model = SetViewModel();
return View(model);
}
[HttpPost]
public async Task<IActionResult> ConvertPdfToImage(PDF_to_Image_ViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the library in demo mode
Licensing.LicenseKey = "3FJDU0ZDU0NTQkddQ1NAQl1CQV1KSkpKU0M=";
// Create the PDF to Image converter instance with default options
PdfToImageConverter pdfToImageConverter = new PdfToImageConverter();
// Optionally set the user password to open a password-protected PDF
if (!string.IsNullOrEmpty(model.UserPassword))
pdfToImageConverter.UserPassword = model.UserPassword;
// Optionally set the owner password to open a password-protected PDF
if (!string.IsNullOrEmpty(model.OwnerPassword))
pdfToImageConverter.OwnerPassword = model.OwnerPassword;
// Set the color space of the resulting images
pdfToImageConverter.ColorSpace = SelectedColorSpace(model.ColorSpace);
// Set the resolution of the resulting images
pdfToImageConverter.Resolution = model.Resolution;
// Set whether image background transparency is enabled
pdfToImageConverter.TransparencyEnabled = model.TransparencyEnabled;
// PDF page number to start conversion from
int startPageNumber = model.StartPageNumber;
// PDF page number to end conversion at
// If 0, conversion continues to the end of the document
int endPageNumber = 0;
if (model.EndPageNumber.HasValue)
endPageNumber = model.EndPageNumber.Value;
byte[] inputPdfBytes = null;
string outputFileName = null;
// If an uploaded file exists, use it with priority
if (model.PdfFile != null && model.PdfFile.Length > 0)
{
try
{
using var ms = new MemoryStream();
await model.PdfFile.CopyToAsync(ms);
inputPdfBytes = ms.ToArray();
}
catch (Exception ex)
{
throw new Exception("Failed to read the uploaded PDF file", ex);
}
outputFileName = Path.GetFileNameWithoutExtension(model.PdfFile.FileName);
}
else
{
// Otherwise, fall back to the URL
string pdfUrl = model.PdfFileUrl?.Trim();
if (string.IsNullOrWhiteSpace(pdfUrl))
throw new Exception("No PDF file provided: upload a file or specify a URL");
try
{
if (pdfUrl.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
{
string localPath = new Uri(pdfUrl).LocalPath;
inputPdfBytes = await System.IO.File.ReadAllBytesAsync(localPath);
}
else
{
using var httpClient = new System.Net.Http.HttpClient();
inputPdfBytes = await httpClient.GetByteArrayAsync(pdfUrl);
}
}
catch (Exception ex)
{
throw new Exception("Could not download the PDF file from URL", ex);
}
outputFileName = Path.GetFileNameWithoutExtension(model.PdfFileUrl);
}
// Convert to images the specified PDF page range
PdfPageImage[] pdfPageImages = pdfToImageConverter.ConvertPdfPagesToImages(inputPdfBytes, startPageNumber, endPageNumber);
if (pdfPageImages.Length == 1)
{
// Return the single image as a downloadable file
outputFileName += ".png";
return File(pdfPageImages[0].ImageData, "image/png", outputFileName);
}
else
{
// Build an in-memory ZIP with all page images
using var zipMs = new MemoryStream();
using (var zip = new System.IO.Compression.ZipArchive(zipMs, System.IO.Compression.ZipArchiveMode.Create, leaveOpen: true))
{
foreach (var pdfPageImage in pdfPageImages)
{
var entry = zip.CreateEntry($"page-{pdfPageImage.PageNumber:000000}.png", System.IO.Compression.CompressionLevel.Fastest);
// Write the image bytes into the ZIP entry
using var entryStream = entry.Open();
entryStream.Write(pdfPageImage.ImageData, 0, pdfPageImage.ImageData.Length);
}
}
outputFileName += ".zip";
// Copy ZIP memory stream to a byte array
byte[] outputZipBytes = zipMs.ToArray();
// Return the ZIP as a downloadable file
return File(outputZipBytes, "application/zip", outputFileName);
}
}
private PdfPageImageColorSpace SelectedColorSpace(string colorSpace)
{
switch (colorSpace)
{
case "RGB":
return PdfPageImageColorSpace.RGB;
case "Mono":
return PdfPageImageColorSpace.Mono;
case "Gray":
return PdfPageImageColorSpace.Gray;
default:
return PdfPageImageColorSpace.RGB;
}
}
private PDF_to_Image_ViewModel SetViewModel()
{
var model = new PDF_to_Image_ViewModel();
HttpRequest request = ControllerContext.HttpContext.Request;
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = request.Scheme;
uriBuilder.Host = request.Host.Host;
if (request.Host.Port != null)
uriBuilder.Port = (int)request.Host.Port;
uriBuilder.Path = request.PathBase.ToString() + request.Path.ToString();
uriBuilder.Query = request.QueryString.ToString();
string currentPageUrl = uriBuilder.Uri.AbsoluteUri;
string rootUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "PDF_to_Image".Length);
model.PdfFileUrl = rootUrl + "/DemoAppFiles/Input/PdfProcessor_Files/PDF_Document.pdf";
return model;
}
}
}