How to Convert PDF to Image in C# (Developer Tutorial)

xeshan6981

Zeeshan Wazir

Posted on April 25, 2024

How to Convert PDF to Image in C# (Developer Tutorial)

In many applications, there arises the need to extract pages from a PDF document and convert them into image files for various purposes such as display, processing, or archival. With IronPDF, a powerful .NET library, converting PDFs to images becomes a straightforward process. In this article, we'll explore how to convert a PDF file to images in C# using IronPDF.

IronPDF - The Ultimate PDF Library

IronPDF empowers developers to create, edit, and manage PDFs within their applications. This library shines in converting HTML, including CSS, JavaScript, and images, into high-quality PDFs. It can also edit existing PDFs, extract text and convert PDF pages to images, and provide security features. IronPDF stands out for its speed and efficiency, making it a valuable tool for developers working with PDFs. Designed for ease of use and efficiency, IronPDF caters to various programming languages, including:

  • C# (.NET)
  • Python
  • Node.js (JavaScript)

This versatility allows developers to leverage their existing skillsets to integrate PDF functionalities into their projects.

Image description

Key Features of IronPDF

IronPDF boasts a comprehensive feature set that caters to diverse PDF-related needs. Here's a closer look at some of its most notable functionalities:

  • HTML to PDF Conversion: IronPDF excels at converting HTML code, including CSS, JavaScript, and images, into high-quality PDFs. This enables developers to generate pixel-perfect PDF renditions of web pages or custom HTML designs.
  • URL to PDF: Need to convert existing web pages to PDFs? IronPDF simplifies the process by allowing you to directly specify the URL of the webpage you wish to convert.
  • Convert PDF to Images: Effortlessly convert all the PDF pages to JPEG images or PNG images with IronPDF's intuitive code snippet. Whether it's a single page or multiple pages, choose the format that suits your needs and get high-quality images in just a few lines of code. It also supports Tiff image format.
  • PDF Editing: IronPDF goes beyond creation. It empowers developers to edit existing PDFs. This includes functionalities like adding headers, footers, watermarks, and page numbers.
  • Form Filling and Data Extraction: IronPDF facilitates the management of interactive PDF forms. Developers can pre-fill forms with data or extract existing data from fillable PDF documents.
  • Image and Text Extraction: IronPDF allows developers to extract text content and images embedded within PDFs. This can be valuable for tasks like data processing or repurposing content from existing PDF documents.
  • Security and Permissions: IronPDF provides control over PDF security by enabling features like password protection and permission settings. This ensures your PDFs remain confidential and accessible only to authorized users.
  • Performance Optimization: IronPDF prioritizes speed and efficiency. It offers multithreading and asynchronous support, ensuring smooth and swift PDF processing even for large or complex documents.
  • Merge and Split Functionality: Need to combine multiple PDFs or split a single PDF into separate documents? IronPDF offers functionalities to streamline these tasks.

Convert PDF Files to Image Format

Step 1: Setting Up the Project

Create a new C# console application using Visual Studio. Follow the steps below:

  1. Launch Visual Studio and click "Create a New Project".
  2. From templates, select "Console App" and click Next.
  3. Give your project a name and choose the location. Then click Next.
  4. From Additional Information, select the .NET Framework and click Create.

Step 2: Installing IronPDF

First, you'll need to install the IronPDF library in your C# project. You can do this via NuGet Package Manager Console in Visual Studio:

PM> Install-Package IronPdf

Alternatively, you can install it from NuGet Package Manager for Solutions:

Image description

Step 3: Converting PDF to Images

Let's have look into the code to convert a PDF into images using IronPDF. Here i'm going to use a 10 page PDF document and rasterize it to images with simply one line of code.

Code Snippet:

The following code snippet convert PDF to JPG and PNG images:

using IronPdf;
using IronSoftware.Drawing; // Required for AnyBitmap class

class Program
{
    static void Main(string[] args)
    {
        // Load the PDF file you want to convert to images
        PdfDocument pdf = PdfDocument.FromFile("Example.pdf");

        // Extract all the pages to a folder as image files (PNG by default)
        pdf.RasterizeToImageFiles(@"C:\image\folder\*.png");

        // Specify dimensions and PDF page ranges (optional)
        pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", 100, 80);

        // Extract all pages as AnyBitmap objects
        AnyBitmap[] pdfBitmaps = pdf.ToBitmap();

        Console.WriteLine("PDF converted to images successfully!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Loading the PDF: We use PdfDocument.FromFile to load the existing PDF, Example.pdf, into the pdf variable.

  2. Converting to Image Files: IronPDF's RasterizeToImageFiles method is used to convert the PDF pages into image files. By default, it saves images as PNG files, but you can specify other formats such as JPEG or TIFF.

  3. Specifying Dimensions and Page Ranges (Optional): You can also specify image dimensions (width and height in pixels) and page ranges for conversion. In the second RasterizeToImageFiles method call, we save images as JPEG files with a width of 100 pixels and a height of 80 pixels.

  4. Extracting as AnyBitmap Objects: The ToBitmap method extracts all pages from the PDF as AnyBitmap objects. This gives you flexibility to further process the images as needed.

For further ready-to-use code snippets, please refer to this code examples page.

Step 4: Running the Application

Build and run the application. If everything is set up correctly, you should see the message "PDF converted to images successfully!" in the console output. Here are the extracted images in png and jpg format:

Image description

Conclusion

Converting PDFs to images in C# using IronPDF provides a simple yet powerful solution. Whether you need to display PDF pages as images, process them further, or save them for archival purposes, IronPDF's RasterizeToImageFiles method makes the task efficient. By following this tutorial, you've learned how to convert PDF documents into image files with ease using IronPDF.

For more detailed information on how to get started with IronPDF, please visit this documentation page.

Unlock the full potential of PDF functionalities with IronPDF! Start with a free trial and discover seamless PDF creation, editing, and conversion. For commercial projects, simply license IronPDF for unlimited access to its powerful features.

💖 💪 🙅 🚩
xeshan6981
Zeeshan Wazir

Posted on April 25, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related