§HOW-TO GUIDE · C# · .NET 8 & 10

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

Video walkthrough · 3:21 · Watch on YouTube · Short · 0:53
§STEP BY STEP

Build it step by step.

  1. 1Install the NuGet packages

    SelectPdf.Universal is the library. The native package carries the Chromium engine for one platform: add the one for the platform you run on.

    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.win-x64
    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.linux-x64
    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.linux-arm64
    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.osx-arm64
  2. 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.cs with the code below.

    terminal
    dotnet new console -n PdfSample
    cd PdfSample
  3. 3Import the namespace

    Program.cs · lines 1–1
    using SelectPdf.Universal;

    Everything used here is in SelectPdf.Universal.

  4. 4Create a PDF to render

    Program.cs · lines 3–11
    // 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, HtmlToPdf converts a three-page report into report.pdf; page-break-before starts the second and third pages. In your application this is the PDF you already have.

  5. 5Load it and set the resolution

    Program.cs · lines 13–16
    // 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;

    PdfRasterizer is the PDF to image converter. Load reads the file; other overloads take a stream, a byte array or a PdfDocument. Resolution is in DPI: 150 renders an A4 page at 1240 by 1754 pixels. The default is 120.

  6. 6Render every page and save the PNGs

    Program.cs · lines 18–23
    // 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]);
    }

    ConvertToImages renders every page and returns one PNG per page as a byte array, in page order. File.WriteAllBytes saves them as page-1.png, page-2.png and page-3.png.

  7. 7Run it

    terminal
    dotnet run

    The program writes report.pdf to the working folder. Without a license key, every page carries a trial watermark; set GlobalProperties.LicenseKey before the first conversion to remove it.

§COMPLETE PROGRAM

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.

Program.cs
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

§OUTPUT

The PDF it produces.

First page of report.pdf, the PDF this sample produces
report.pdf, page 1

Download report.pdf ↓ the file this sample produced, unedited

§VARIATIONS

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.

FirstPageThumbnail.cs
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.

JpegFiles.cs
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.

GrayscaleInMemory.cs
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]);
    }
}
§TROUBLESHOOTING

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

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
?FAQ

Questions, answered.

Short answers about this guide. The SelectPdf.Universal documentation covers every class and option.

Create a PdfRasterizer, load the PDF with Load, set Resolution and call ConvertToImages. It returns one PNG per page as a byte array; write each one with File.WriteAllBytes.
Set StartPageNumber and EndPageNumber to the page you need, counted from 1, before calling ConvertToImages or Save. EndPageNumber = 0, the default, means up to the last page.
Yes. Set ImagesFormat to PdfImageFormat.Jpeg. PdfImageFormat also has Bmp and Tiff, and ConvertToTiff bundles all the selected pages in one multi-frame TIFF.
No. The PDF to image converter is part of the commercial SelectPdf.Universal library. The Community Edition is HTML to PDF only.