USING IRONPDF

How to Convert PDF to PNG in C#

Updated March 17, 2024
Share:

In today's digital age, the need to convert PDF files to various image formats is becoming increasingly common. Whether you want to extract images from a PDF document, convert specific pages to PNG images, or manipulate PDF files in other ways, IronPDF provides a powerful solution for .NET developers.

This article will explore the capabilities of IronPDF and demonstrate how to convert PDF files to PNG images using different approaches.

Key Features of IronPDF's PDF-to-Images Rasterizing Solution

  • IronPDF offers a feature-rich solution for converting PDF files to raster images, packed with the following features:
  • Wide Range of Image Formats: IronPDF allows you to export image files in formats such as JPEG, PNG, BMP, and Bitmap. These formats are suitable for various use cases, including high-resolution prints and web publishing.
  • Individual Image Files for Each Page: The solution generates one image file per page, making it easy to manage and work with the resulting images.
  • Customizable Image Dimensions: You can specify the dimensions of the output images to best suit your project requirements.
  • Selective Page Conversion: IronPDF enables you to choose specific PDF pages or page ranges for conversion, providing flexibility and control over the process.
  • Easy Installation: The IronPDF library is straightforward to install, allowing you to start converting PDF files immediately.
  • Convenient Licensing Options: IronPDF offers quick and easy licensing options, simplifying the integration of the solution into your projects.
  • Outstanding Performance: IronPDF stands out among other PDF-to-raster file solutions, delivering exceptional performance and results.

In addition to the features discussed above, IronPDF offers various methods and options for customizing the PDF to PNG conversion process. For example, you can specify attributes such as image format, image quality, DPI, and page size. This allows you to tailor the output image according to your specific requirements.

Create a C# Application

Creating a new Console Application project in Visual Studio involves the following steps:

  1. Open Visual Studio: Launch the Visual Studio IDE on your computer.
  2. Create a new project: From the "File" menu, select "New" and then "Project" from the dropdown menu. This will open a new screen.
  3. Choose the project type: In the "Create a new project" screen, select "Console App" and hit Next button.

    How to Convert PDF to PNG in C#, Figure 1: Create a Console App in Visual Studio Create a Console App in Visual Studio

  4. Configure your new project: Click "Next" after selecting the appropriate template. In the next screen, you'll have to enter a name for your project, decide its location, and optionally choose a solution name if you plan on having multiple projects under the same solution.

    How to Convert PDF to PNG in C#, Figure 2: Configure your new project Configure your new project

  5. Choose the target framework: After you click "Next", select the target framework for your project. Choose the .NET 7 version from the dropdown.

    How to Convert PDF to PNG in C#, Figure 3: .NET Framework selection .NET Framework selection

  6. Create the project: Once configuring all the settings, click the Create button. Visual Studio will now create a new Console Application project with a Program.cs file containing a simple "Hello World" program by default.

Installing IronPDF in C#

To install IronPDF in a C# project, you can follow these steps

  1. Open your C# project in Visual Studio.
  2. Right-click on your project in the Solution Explorer and select "Manage NuGet Packages."
  3. In the NuGet Package Manager window, select the "Browse" tab.
  4. In the search box, type "IronPdf" and press Enter.
  5. Locate the IronPdf package in the search results and click on it.
  6. On the right-hand side, click the "Install" button to begin the installation process.
  7. Review the package installation details and click the "I Accept" button to accept the package's license terms.
  8. Visual Studio will download and install the IronPDF package and its dependencies into your project.
  9. Once the installation is complete, you can start using IronPDF in your C# code.

However, you can also install IronPDF using NuGet Package Manager Console using the following command:

Install-Package IronPdf

To verify that IronPDF is successfully installed, you can add a reference to the IronPdf namespace in your code file:

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

This allows you to access the IronPDF classes, methods, and properties in your project.

You are now ready to utilize IronPDF's functionality to work with PDF files programmatically in your C# application. Remember to consult the IronPDF documentation and the examples provided by the library to explore the various features and capabilities it offers.

Converting PDF Files to Images with IronPDF

When working with PDF files in C#, IronPDF provides a convenient solution for converting these files to images. This article will explore the capabilities of IronPDF and demonstrate how to convert PDF files to images using different approaches.

Loading a PDF File

To begin, load a PDF file into this project. IronPDF offers a straightforward method called FromFile in the PdfDocument class to accomplish this task. The following code sample illustrates how 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 file to Images

Once the PDF file is loaded, IronPDF provides the RasterizeToImageFiles method to convert PDF pages to image format. With a single line of code, it is possible to convert the entire PDF document to JPG images:

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

The converted files will be saved in the specified path. IronPDF efficiently converts all the pages of the PDF document to JPG images. The method RasterizeToImageFiles handles the conversion process, automatically assigning incremental numerical names to the images.

How to Convert PDF to PNG in C#, Figure 4: The output images The output images

Converting Specific PDF Pages

The RasterizeToImageFiles method also allows for more control over PDF to JPG conversion. You can also convert a particular page from a PDF document to an image. The following code sample shows how to convert PDF pages in a specific range, from page 1 to 10:

IEnumerable<int> pageIndexes = Enumerable.Range(0, 10);
pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", pageIndexes, 850, 650, IronPdf.Imaging.ImageType.Default, 300);
IEnumerable<int> pageIndexes = Enumerable.Range(0, 10);
pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", pageIndexes, 850, 650, IronPdf.Imaging.ImageType.Default, 300);
Dim pageIndexes As IEnumerable(Of Integer) = Enumerable.Range(0, 10)
pdf.RasterizeToImageFiles("C:\image\folder\example_pdf_image_*.jpg", pageIndexes, 850, 650, IronPdf.Imaging.ImageType.Default, 300)
VB   C#

How to Convert PDF to PNG in C#, Figure 5: Convert specific pages in a PDF file into images Convert specific pages in a PDF file into images

In this example, several parameters are specified:

  • First Parameter: A valid path with an optional image extension is provided as a string.
  • Second Parameter: pageIndexes indicates the page range to be converted to JPG images programmatically.
  • Third Parameter: The maximum image width in pixels is set.
  • Fourth Parameter: The maximum image height in pixels is specified.
  • Fifth Parameter: The image type is set to default, which will save images in PNG image format if the extension is not explicitly mentioned in the path. Other available formats include PNG, GIF, TIFF, JPG, and Bitmap.
  • Sixth Parameter: The desired resolution of the output image files can be set. Note that DPI will be ignored in Linux and macOS.

Converting URL to PDF and then PDF to Images

IronPDF also offers the ability to convert a URL to PDF and subsequently save each page of the generated PDF as a separate image file. This is particularly useful when capturing products or web page content as images. The following code snippet demonstrates this process by rendering an Amazon website page to a pixel-perfect PDF and then converting each page 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 PNG in C#, Figure 6: The converted images from a URL The converted images from a URL

In this example, the ChromePdfRenderer class is used to render the URL as a PDF. Subsequently, the RasterizeToImageFiles method is called to convert each page of the generated PDF to a separate JPG file.

IronPDF, with its powerful capabilities and easy-to-use API, provides a comprehensive solution for converting PDF files to images programmatically in C#. By incorporating IronPDF into your projects, you can effortlessly handle PDF-to-image conversions, extract embedded images, and manipulate PDF documents.

Conclusion

This article explored how to utilize IronPDF for .NET Framework to convert PDF documents into JPG image file format. The RasterizeToImageFiles method, as demonstrated in the code examples above, generates images that incorporate the document name and page number. With IronPDF, you can convert PDF pages into various image formats, including PNG, JPG, GIF, and more.

The IronPDF Library grants users complete control over the output image format, dimensions, and resolution. Additionally, IronPDF offers an array of other PDF manipulation tools such as page rotation, text modification, margin adjustment, and more. To delve deeper into the capabilities of IronPDF for .NET and access additional features for PDF file manipulation, please refer to the following example to convert HTML to a PDF file.

While IronPDF .NET Library is free for development purposes, it requires licensing for commercial usage. You can download the zip file for the IronPDF .NET library from this homepage and give it a try.

< PREVIOUS
How to Display PDF Embedded Text in .NET MAUI
NEXT >
PDF vs PDFA (How It Works For Developers)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >