Convert PDF pages to PNG images in C#
PdfRasterizer is the PDF to image converter: load a PDF, set the resolution in DPI and call ConvertToImages. You get one encoded PNG per page as a byte array, ready to write to a file, a stream or a web response.
Compiled and run: Linux ARM64 · Linux x64 · macOS ARM64 · Windows x64
Build it step by step.
-
1Install the NuGet packages
SelectPdf.Universalis the library. The native package carries the Chromium engine for one platform: add the one for the platform you run on.dotnet add package SelectPdf.Universal dotnet add package SelectPdf.Universal.Native.win-x64
dotnet add package SelectPdf.Universal dotnet add package SelectPdf.Universal.Native.linux-x64
dotnet add package SelectPdf.Universal dotnet add package SelectPdf.Universal.Native.linux-arm64
dotnet add package SelectPdf.Universal dotnet add package SelectPdf.Universal.Native.osx-arm64
-
2Create a console app
Any .NET 8 or .NET 10 project works. To follow along, create a console app, run the install commands above inside its folder, then replace
Program.cswith the code below.dotnet new console -n PdfSample cd PdfSample
-
3Import the namespace
using SelectPdf.Universal;
Everything used here is in
SelectPdf.Universal. -
4Create a PDF to render
// 1. Make a three-page report to render. HtmlToPdf converter = new HtmlToPdf(); PdfDocument report = converter.ConvertHtmlString(""" <h1 style="color: #1f6feb">Quarterly report</h1><p>Revenue, costs and forecast.</p> <h2 style="page-break-before: always">Costs</h2><p>Details by department.</p> <h2 style="page-break-before: always">Forecast</h2><p>Next quarter.</p> """); report.Save("report.pdf"); report.Close();To keep the sample self-contained,
HtmlToPdfconverts a three-page report intoreport.pdf;page-break-beforestarts the second and third pages. In your application this is the PDF you already have. -
5Load it and set the resolution
// 2. Load the PDF into the PDF to image converter and set the resolution. var rasterizer = new PdfRasterizer(); rasterizer.Load("report.pdf"); rasterizer.Resolution = 150;PdfRasterizeris the PDF to image converter.Loadreads the file; other overloads take a stream, a byte array or aPdfDocument.Resolutionis in DPI: 150 renders an A4 page at 1240 by 1754 pixels. The default is 120. -
6Render every page and save the PNGs
// 3. Render every page to PNG: one byte array per page. byte[][] pages = rasterizer.ConvertToImages(); for (int i = 0; i < pages.Length; i++) { File.WriteAllBytes($"page-{i + 1}.png", pages[i]); }ConvertToImagesrenders every page and returns one PNG per page as a byte array, in page order.File.WriteAllBytessaves them aspage-1.png,page-2.pngandpage-3.png. -
7Run it
dotnet run
The program writes
report.pdfto the working folder. Without a license key, every page carries a trial watermark; setGlobalProperties.LicenseKeybefore the first conversion to remove it.
The complete Program.cs.
This is the exact file our samples pipeline compiled and ran on Linux ARM64 and Linux x64 and macOS ARM64 and Windows x64 to produce the output on this page.
using SelectPdf.Universal;
// 1. Make a three-page report to render.
HtmlToPdf converter = new HtmlToPdf();
PdfDocument report = converter.ConvertHtmlString("""
<h1 style="color: #1f6feb">Quarterly report</h1><p>Revenue, costs and forecast.</p>
<h2 style="page-break-before: always">Costs</h2><p>Details by department.</p>
<h2 style="page-break-before: always">Forecast</h2><p>Next quarter.</p>
""");
report.Save("report.pdf");
report.Close();
// 2. Load the PDF into the PDF to image converter and set the resolution.
var rasterizer = new PdfRasterizer();
rasterizer.Load("report.pdf");
rasterizer.Resolution = 150;
// 3. Render every page to PNG: one byte array per page.
byte[][] pages = rasterizer.ConvertToImages();
for (int i = 0; i < pages.Length; i++)
{
File.WriteAllBytes($"page-{i + 1}.png", pages[i]);
}
Open the project on GitHub → this program and the variations below, ready to dotnet run
The PDF it produces.
Download report.pdf ↓ the file this sample produced, unedited
Variations you will need next.
Each variation is a separate file in the same project, compiled and run with the program above.
A thumbnail of the first page
StartPageNumber and EndPageNumber select the pages, counted from 1; an end of 0, the default, means the last page. Both set to 1 render only the cover. At 72 DPI an A4 page becomes a 595 by 842 pixel preview. Load(doc) reads the converted PdfDocument directly, so no file is written in between.
using SelectPdf.Universal;
static class FirstPageThumbnail
{
public static void Run()
{
HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertHtmlString("""
<h1>Product catalog</h1><p>Spring collection.</p>
<h2 style="page-break-before: always">Prices</h2><p>All items.</p>
""");
// load the document straight from memory, no file in between
var rasterizer = new PdfRasterizer();
rasterizer.Load(doc);
// only page 1, at 72 DPI for a small preview
rasterizer.StartPageNumber = 1;
rasterizer.EndPageNumber = 1;
rasterizer.Resolution = 72;
byte[][] pages = rasterizer.ConvertToImages();
File.WriteAllBytes("thumbnail.png", pages[0]);
doc.Close();
}
}
Write JPG files with Save
Save writes one file per page into ImagesPath, named ImagesPrefix plus the page number, in ImagesFormat. Here that gives album-pages/page-1.jpeg and page-2.jpeg. The folder must exist before you call Save.
using SelectPdf.Universal;
static class JpegFiles
{
public static void Run()
{
HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertHtmlString("""
<h1>Photo album</h1><p>Summer trip.</p>
<h2 style="page-break-before: always">Day two</h2><p>The coast.</p>
""");
doc.Save("album.pdf");
doc.Close();
// Save() writes one file per page into ImagesPath, named ImagesPrefix + page number
var rasterizer = new PdfRasterizer();
rasterizer.Load("album.pdf");
rasterizer.ImagesFormat = PdfImageFormat.Jpeg;
Directory.CreateDirectory("album-pages"); // the folder must exist
rasterizer.ImagesPath = "album-pages";
rasterizer.ImagesPrefix = "page-";
rasterizer.Save();
}
}
Grayscale images, from bytes to a stream
The PDF comes in as a byte array, as it would from a database or an upload, and Load reads it from a MemoryStream. PdfRasterizerColorSpace.Gray renders in shades of gray. Each item that ConvertToImages returns is a complete PNG, so you can write it to any stream: a file, a response body or blob storage.
using SelectPdf.Universal;
static class GrayscaleInMemory
{
public static void Run()
{
HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertHtmlString("<h1>Scanned letter</h1><p>Black text on white.</p>");
byte[] pdf = doc.Save(); // the PDF as bytes, e.g. from a database or an upload
doc.Close();
// load from a stream and render in shades of gray
var rasterizer = new PdfRasterizer();
rasterizer.Load(new MemoryStream(pdf));
rasterizer.ColorSpace = PdfRasterizerColorSpace.Gray;
byte[][] pages = rasterizer.ConvertToImages();
// each page is an encoded PNG: write it to any stream (a response body, blob storage...)
using FileStream file = File.Create("letter-gray.png");
file.Write(pages[0]);
}
}
Common problems and fixes.
Save throws: the images path does not exist
Save does not create ImagesPath. Create the folder first with Directory.CreateDirectory, as the JPG variation does.
The images are blurry or too large
Raise or lower Resolution. The docs suggest 72 DPI for thumbnails, 120 for screen display, 150 for decent print and 300 for high-quality print; doubling the DPI quadruples the pixels per page.
The source PDF is password-protected
Use a Load overload that takes a password, for example Load(fileName, password), or set UserPassword before the conversion.
You need all pages in one file
ConvertToTiff returns all the selected pages as one multi-frame TIFF, and SaveTiff writes it into ImagesPath.
Good to know.
- The PDF to image converter renders PDF pages to PNG, JPG, BMP or TIFF. Source
- ConvertToImages returns one byte array per rendered page, encoded in ImagesFormat (PNG by default). Source
- Resolution is the resolution of the generated images in DPI; the default is 120. Source
- File size and memory scale quadratically with DPI: doubling the DPI quadruples the pixel count per page. Source
- The cross-platform Community Edition is HTML to PDF only; PDF to image is part of the commercial SelectPdf.Universal package. Source
Questions, answered.
Short answers about this guide. The SelectPdf.Universal documentation covers every class and option.