Skip to content

Instantly share code, notes, and snippets.

@irondarrius
Last active January 30, 2025 06:55
Show Gist options
  • Save irondarrius/e2ababa5e2464be3c54392289ae0cbe9 to your computer and use it in GitHub Desktop.
Save irondarrius/e2ababa5e2464be3c54392289ae0cbe9 to your computer and use it in GitHub Desktop.
Convert PDFs to Images in C#

Convert a PDF to an Image in C# Using IronPDF

Introduction

This guide demonstrates how to convert a PDF document to an image format (e.g., PNG, JPG) using the IronPDF library in C#. This is useful for creating visual previews or thumbnails of PDF content.


Prerequisites

To follow along, ensure you have the following:

  • IronPDF NuGet Package: Install it via NuGet:
    Install-Package IronPdf
  • A sample PDF file to use for conversion.
  • A C# development environment, such as Visual Studio.

using IronPdf;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Path to the PDF file
string pdfPath = "sample.pdf";
// Load the PDF document
var pdf = PdfDocument.FromFile(pdfPath);
// Specify the output folder for the images
string outputFolder = "output_images";
// Ensure the output directory exists
Directory.CreateDirectory(outputFolder);
// Save each page of the PDF as a separate image
pdf.RasterizeToImageFiles(Path.Combine(outputFolder, "page_{page}.png"), 300); // 300 DPI for high quality
Console.WriteLine("PDF to image conversion completed.");
}
}
Key Notes
  • RasterizeToImageFiles: Automatically converts all pages of the PDF into separate image files.
  • DPI (dots per inch): Higher DPI values result in better image quality but larger file sizes.

Advanced Use Cases

Here are a few additional tips for more advanced scenarios:

  1. Customizing Image Formats Change the output format (e.g., save as JPG instead of PNG):

    pdf.RasterizeToImageFiles(Path.Combine(outputFolder, "page_{page}.jpg"), 300);
  2. Working with Specific Pages Convert only selected pages instead of the entire document:

    var image = pdf.RasterizePage(2, 300); // Convert only page 2
    image.SaveAs(Path.Combine(outputFolder, "page_2.png"));
  3. Combining Output with Other Features IronPDF allows you to annotate or edit PDFs before converting them to images.

using IronPdf;
using System;
using System.IO;
// This example shows how to save the converted images as JPG files instead of PNG
string pdfPath = "sample.pdf";
var pdf = PdfDocument.FromFile(pdfPath);
string outputFolder = "output_images";
Directory.CreateDirectory(outputFolder);
pdf.RasterizeToImageFiles(Path.Combine(outputFolder, "page_{page}.jpg"), 300);
Console.WriteLine("PDF to JPG conversion completed.");
using IronPdf;
using System;
using System.IO;
// This example demonstrates how to convert only a specific page of the PDF:
string pdfPath = "sample.pdf";
var pdf = PdfDocument.FromFile(pdfPath);
string outputFolder = "output_images";
Directory.CreateDirectory(outputFolder);
var image = pdf.RasterizePage(2, 300); // Convert page 2
image.SaveAs(Path.Combine(outputFolder, "page_2.png"));
Console.WriteLine("Page 2 converted to PNG.");
using IronPdf;
using System;
using System.IO;
// This example shows how to annotate a PDF before converting it to images:
string pdfPath = "sample.pdf";
var pdf = PdfDocument.FromFile(pdfPath);
// Add a header to the PDF
pdf.StampText("Confidential", new TextStampOptions
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top,
FontSize = 48,
Color = System.Drawing.Color.Red
});
string outputFolder = "output_images";
Directory.CreateDirectory(outputFolder);
pdf.RasterizeToImageFiles(Path.Combine(outputFolder, "page_{page}.png"), 300);
Console.WriteLine("PDF annotated and converted to images.");

Output Example

After running the code, the images will be saved in the output_images directory. A sample folder structure:

/output_images/
  - page_1.png
  - page_2.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment