How to Convert PDF to JPG in .NET

Converting PDF files or specific pages to image files such as JPG format, PNG, Bitmap or TIFF has numerous applications in the software industry. Sometimes, you need to convert whole PDF file pages to separate images or sometimes specific page images need to be extracted. Only option to convert PDF to JPG is taking a screenshot and saving it. This can be a time consuming and tedious task. There are many online converter available which convert PDF to JPG online. Imagine, you require to convert PDF documents to images in your C# project. In C#, traditional code can be very difficult to make PDF to JPG conversion. However, with improving technology and emergence of various PDF libraries this task is now easy. Here we will use IronPDF for C# .NET to convert PDF to JPG images.

IronPDF - .NET Library

IronPDF for .NET is a library which allows users to create, edit, and manage PDF files. It is very popular among C# developers because of its PDF generation component, which allows them to work with PDF files without Adobe Acrobat installed. IronPDF for .NET allows conversion between different formats like HTML to PDF, URL to PDF, Images to PDF.

It also supports adding custom headers & footers, digital signatures, annotations and attachments, user and owner passwords, and other security options. IronPDF has a fast Chromium Engine for better rendering experience. It renders a pixel perfect PDF. It also provides full multithreading and async support.

Now, we will discuss how to convert PDF format to image format like PNG or JPG image programmatically using IronPDF.

Prerequisites

Before we start, Visual Studio 2022 (current version) needs to be downloaded and installed. It is necessary for building C# apps. This will help set up the .NET environment, and we will be ready to make PDF to JPG converter.

IronPDF Installation

To install IronPDF there are multiple ways:

  1. You can download IronPDF using the NuGet Package Manager into you C# project created using Visual Studio. Access NuGet Package Manager via Tools or by Right-Clicking Solution Explorer. Browse for IronPDF package and install.
  2. Another way to install IronPDF is by directly downloading it from the NuGet website.

Convert PDF File to Images using IronPDF

Load PDF Document

To load a PDF file from a local location into our project, IronPDF provides a FromFile method present in PdfDocument class. The following code example helps to open an existing PDF file for editing:


PdfDocument pdf = PdfDocument.FromFile("Example.pdf");

PdfDocument pdf = PdfDocument.FromFile("Example.pdf");
Dim pdf As PdfDocument = PdfDocument.FromFile("Example.pdf")
VB   C#

Convert PDF Document to Images

Now, the file is opened for editing. IronPDF provides a RasterizeToImageFiles method to convert PDF pages to Image format. With the following one line of code we will convert the entire PDF document into JPG images.


// Extract all pages to a folder as image files
pdf.RasterizeToImageFiles(@"C:\image\folder\*.jpg");

// Extract all pages to a folder as image files
pdf.RasterizeToImageFiles(@"C:\image\folder\*.jpg");
' Extract all pages to a folder as image files
pdf.RasterizeToImageFiles("C:\image\folder\*.jpg")
VB   C#

The converted files from the above code will be saved in the given path. The PDF contains 562 pages and IronPDF takes no time to convert all PDF pages to JPG images. The RasterizeToImageFiles method does all the hard work and the name of images is a digit which starts from 1 and is incremented by each page.

How to Convert PDF to JPG in .NET: Figure 2

Convert Specific PDF Pages

RasterizeToImageFiles method provides other options too for more control over PDF Pages to JPG conversion. The following code helps to convert PDF pages in a range from page 11 to 21.


    // Set the Page Range
    IEnumerable PageIndexes = Enumerable.Range(10,20);

    // Path, PageIndexes, ImageType and Dimensions maybe specified
    pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", PageIndexes, 850, 650, IronPdf.Imaging.ImageType.Default, 300);

    // Set the Page Range
    IEnumerable PageIndexes = Enumerable.Range(10,20);

    // Path, PageIndexes, ImageType and Dimensions maybe specified
    pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", PageIndexes, 850, 650, IronPdf.Imaging.ImageType.Default, 300);
' Set the Page Range
	Dim PageIndexes As IEnumerable = Enumerable.Range(10,20)

	' Path, PageIndexes, ImageType and Dimensions maybe specified
	pdf.RasterizeToImageFiles("C:\image\folder\example_pdf_image_*.jpg", PageIndexes, 850, 650, IronPdf.Imaging.ImageType.Default, 300)
VB   C#

In the above convert PDF to JPG code example, a lot of things are happening. Let's have a look at them one by one.

  • First Parameter: A valid path with an optional image extension is provided as a string.
  • Second Parameter: PageIndexes provide page range which needs to be converted to JPG image programmatically.
  • Third Parameter: Here we specify the maximum Image width in pixels
  • Fourth Parameter: Here we specify the maximum height of the Image in pixels
  • Fifth Parameter: Image type default will save images in PNG format if the extension is not mentioned in the path. There are other formats available like: PNG, GIF, TIFF, JPG and Bitmap
  • Sixth Parameter: Here we can set the desired resolution of the output image files. Except Windows, DPI will be ignored in Linux and macOS.

The JPG conversion output will be:

How to Convert PDF to JPG in .NET: Figure 3

Convert URL to PDF and then PDF to Images

Sometimes there is a need to capture the products listed on a website to images for some purpose. Let's say there are hundreds of products listed on a website page. Taking screenshots will be a time-consuming and hectic task. IronPDF provides the facility to convert a URL to PDF and using the generated PDF document we can again save each page to Image.

The following code takes the Amazon website page as URL and renders it to pixel perfect PDF. After that each page of the generated PDF is converted to a separate JPG file.


    using IronPdf;

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");

    pdf.RasterizeToImageFiles(@"C:\image\folder\amazon_pdf_image_*.jpg");

    using IronPdf;

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");

    pdf.RasterizeToImageFiles(@"C:\image\folder\amazon_pdf_image_*.jpg");
Imports IronPdf

	Private renderer As New ChromePdfRenderer()
	Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20")

	pdf.RasterizeToImageFiles("C:\image\folder\amazon_pdf_image_*.jpg")
VB   C#

How to Convert PDF to JPG in .NET: Figure 4

Conclusion

In this article, we learned how to convert PDF documents to JPG images using IronPDF for .NET Framework. The RasterizeToImageFiles method produces images which contain the page number along with the document name, as shown in the above code examples. IronPDF can convert PDF pages into images in different formats: PNG, JPG, GIF and many more.

IronPDF Library provides full control over the output image format, dimensions, and resolution to its users. IronPDF also provides other PDF tools such as rotate PDF pages, change PDF text, set margins etc. To learn more about IronPDF for .NET and to access additional features to manipulate PDF files, please refer to the following link. For more information on how to convert PDF to different format images, visit this link.

IronPDF .NET Library is free for development but needs to be licensed for commercial use.

Download the zip file for IronPDF .NET library from this link and give it a try.