Convert PDF to Images in C# | .NET

Converting PDF files to images is a common requirement for applications that involve document previews, thumbnails generation, or PDF content archiving. For .NET developers, the Spire.PDF library offers a robust and efficient way to achieve this PDF to image conversion with high fidelity. Its key advantages include:

  • High-Fidelity Rendering: Preserve original layouts and graphics.
  • Multi-Format Support: Export to PNG, JPEG, BMP, TIFF, and more.
  • Cross-Platform: Works with .NET Framework, .NET Core, and .NET 6+.

Visual guide for converting PDF to images Using C#

In this guide, you’ll learn how to use the Spire.PDF for .NET library to convert PDF to JPG or PNG images in C#, with installation guide, practical code snippets, and conversion optimization tips.

Install the PDF to Image Converter Library

Before you can start using C# to convert PDF to PNG or PDF to JPG, it’s necessary to install the Spire.PDF for .NET library first. You can do this via NuGet Package Manager:

  • Open your project in Visual Studio
  • Go to “Tools > NuGet Package Manager > Manage NuGet Packages for Solution”
  • Search for "Spire.PDF"
  • Select and install the package

Alternatively, use the Package Manager Console:

PM> Install-Package Spire.PDF

Once installed, you're ready to start writing code to handle PDF to image conversions.

Convert PDF to JPG Images in C#: Basic Example

The SaveAsImage(int pageIndex, PdfImageType type) method of the PdfDocument class can be used to convert a specified PDF page to an image. The two parameters are:

  1. pageIndex (Int32)
  • Description: The index of the PDF page to be converted (page index starts from 0).
  • Example:
    • 0 = First page
    • 1 = Second page
  1. type (PdfImageType)
  • Description: Specifies the type of the PdfImage.
  • Enum Values:
    • Bitmap
    • Metafile

The following code demonstrates how to convert the first page of a PDF to a JPEG image using Spire.PDF:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoJPG
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            // Load a sample PDF document
            pdf.LoadFromFile("input.pdf");

            // Convert the first page to a bitmap image
            Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap);

            // Save the image as a JPG file
            image.Save("PdfToJPG.jpg", ImageFormat.Jpeg);

            // Disposes PDF resources
            pdf.Dispose();
        }
    }
}

Convert a PDF page to a JPG image

Advanced PDF to Image Conversion Options

1. Set Image Resolution

Spire.PDF offers another overload of the SaveAsImage() method that takes four parameters: the page index, image type, horizontal (dpiX) and vertical (dpiY) resolution. This allows you to control the quality and size of the output image by customizing the DPI settings.

The following code example converts the first page of a PDF to a high-resolution PNG image (300 DPI):

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            // Load a sample PDF document
            pdf.LoadFromFile("input.pdf");

            // Convert the first page to an image and with specified image resolution
            Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 300, 300);

            // Save the image as a PNG file
            image.Save("PdfToPNG.png", ImageFormat.Png);

            // Disposes PDF resources
            pdf.Dispose();

        }
    }
}

Convert a PDF page to a PNG image

Note: Higher DPI improves image quality but slow down conversion.

2. Convert an Entire PDF File to Images

Loop through all pages in the PDF and then convert each one to a separate image.

// Loop through all pages in the PDF
for (int i = 0; i < pdf.Pages.Count; i++)
{
    // Convert each page to an image and set the image Dpi
    Image image = pdf.SaveAsImage(i, PdfImageType.Bitmap, 300, 300);

    // Save the image as PNG format to a specified folder
    string file = string.Format("Image\\ToImage-{0}.png", i);
    image.Save(file, ImageFormat.Png);
}

3. Convert to Different Image Formats

By changing the ImageFormat parameter of the Image.Save() method, you can convert PDF to JPG, PNG, BMP, or Gif formats:

// Save to JPEG format
image.Save("PDFtoJPG.jpg", ImageFormat.Jpeg);

// Save to PNG format
image.Save("PDFtoPNG.png", ImageFormat.Png);

// Save to BMP format
image.Save("PDFtoBMP.bmp", ImageFormat.Bmp);

// Save to EMF format
image.Save("PDFtoEMF.emf", ImageFormat.Emf);

// Save to GIF format
image.Save("PDFtoGIF.gif", ImageFormat.Gif);

4. Using MemoryStream

For scenarios requiring stream handling, use the following code:

// Create a MemoryStream object
MemoryStream ms = new MemoryStream();

// Convert the first page to an image with specified dpi
Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 300, 300);

// Save the image to stream in PNG format
image.Save(ms, ImageFormat.Png);

// Write the Stream object to a byte array for further processing
byte[] imageBytes = ms.ToArray();

Performance Optimization Tips

  • Memory Management: Convert pages one by one to avoid memory overload for large PDFs.
  • DPI Optimization: High DPI settings improve image quality, but also increase file size and processing time.
  • Dispose Resources: Call the Dispose() method of PdfDocument class to free resources.

Conclusion

Spire.PDF provides a straightforward and efficient way to convert PDF files to images in C#. With its rich feature set, high-quality rendering, and support for multiple image formats, it’s a reliable choice for both simple and complex PDF-to-image conversion tasks. By following the examples in this article, you can quickly implement PDF converter functionality in your .NET applications.

FAQs

Q1: How to remove the watermarks on the output images?

A: You can request a free 30-day trial license here to remove the red watermark and fully evaluate the Spire.PDF library. Or you can use its Free version (with certain page limitations).

Q2: Can I convert PDFs to multi-page TIFF files with Spire.PDF?

A: Yes! A detailed guide for this can be found at: Convert PDF to TIFF in C#

Q3: Can I convert PDF to SVG using Spire.PDF?

A: Yes! Spire.PDF for .NET offers the SaveToFile(String, FileFormat.SVG) method to convert a PDF file to a SVG file. You can check out the article for a comprehensive guide.

Q4. Where can I find additional documentation or support?