DEV Community

jelizaveta
jelizaveta

Posted on

Convert PDF to JPG in C#: High-Quality Rendering with Spire.PDF

In document management systems, electronic archiving platforms, and online document preview applications, converting PDF files to images is a fundamental and essential capability. Whether you need to generate document thumbnails, display content across different platforms, or digitally archive historical records, PDF-to-image conversion plays a critical role. For .NET developers, Spire.PDF for .NET provides a powerful and efficient solution that enables high-fidelity PDF-to-image conversion without relying on third-party software such as Adobe Acrobat.

This article starts with the basics and gradually explores more advanced scenarios, including DPI control and stream-based processing, demonstrating how to convert PDF files to JPG images using C# and Spire.PDF.

Environment Setup

Before writing any code, install Spire.PDF into your project through NuGet. In Visual Studio, navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution , search for Spire.PDF , and install it. Alternatively, run the following command in the Package Manager Console:

PM> Install-Package Spire.PDF
Enter fullscreen mode Exit fullscreen mode

It is important to note that the free edition of Spire.PDF includes certain limitations, such as converting only a limited number of pages. For full functionality in production environments, a commercial license is required.

The Core Method: SaveAsImage

The SaveAsImage() method provided by the PdfDocument class is the foundation of PDF-to-image conversion. It offers several overloads to accommodate different requirements:

  • SaveAsImage(int pageIndex, PdfImageType imageType) Converts a specified page to an image, where pageIndex is a zero-based page index and imageType defines the output image format (typically PdfImageType.Bitmap).
  • SaveAsImage(int pageIndex, PdfImageType imageType, int dpiX, int dpiY) Converts a page while specifying horizontal and vertical DPI values, allowing you to control image quality and file size.

Basic Conversion: Export a Single Page

The following example demonstrates the simplest way to convert the first page of a PDF document to a JPG image:

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

namespace ConvertSpecificPageToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
            Image image = doc.SaveAsImage(0, PdfImageType.Bitmap);
            image.Save("ToJPG.jpg", ImageFormat.Jpeg);
            doc.Dispose();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The conversion workflow consists of four straightforward steps:

  1. Create a PdfDocument instance.
  2. Load the target PDF file.
  3. Call SaveAsImage() to render the specified page into a System.Drawing.Image object.
  4. Save the image as a JPG file using the Image.Save() method.

Batch Conversion: Processing Multi-Page PDFs

In real-world applications, you often need to convert all pages or a subset of pages within a PDF document. By iterating through the doc.Pages collection and calling SaveAsImage() for each page, you can convert an entire document:

for (int i = 0; i < doc.Pages.Count; i++)
{
    Image image = doc.SaveAsImage(i, PdfImageType.Bitmap);
    string fileName = string.Format("Output\\ToJPG-{0}.jpg", i);
    image.Save(fileName, ImageFormat.Jpeg);
}
Enter fullscreen mode Exit fullscreen mode

If only a specific page range is required—for example, the first three pages—you can limit the loop accordingly:

for (int i = 1; i <= 3; i++)
{
    Image image = doc.SaveAsImage(i, PdfImageType.Bitmap);
    string fileName = string.Format("Output\\ToJPG-{0}.jpg", i);
    image.Save(fileName, ImageFormat.Jpeg);
}
Enter fullscreen mode Exit fullscreen mode

DPI Control: Ensuring High-Quality Output

DPI (Dots Per Inch) is one of the most important factors affecting image clarity. Higher DPI values produce images with more pixels and finer details, but they also increase file size. Spire.PDF allows developers to precisely control output resolution by specifying dpiX and dpiY when calling SaveAsImage():

Image image = doc.SaveAsImage(0, PdfImageType.Bitmap, 300, 300);
image.Save("ToJPG.jpg", ImageFormat.Jpeg);
Enter fullscreen mode Exit fullscreen mode

Different use cases require different DPI settings:

  • 96–150 DPI : Suitable for web previews and online viewing.
  • 300 DPI : Recommended for printing and professional document output.
  • 600 DPI or higher : Ideal for high-resolution archiving and preserving fine document details.

Choosing the right DPI involves balancing image quality against storage and bandwidth requirements.

Stream-Based Processing: Saving to Memory

In some scenarios—such as uploading images directly to cloud storage or returning image data through a Web API—saving the converted image to disk is not the most efficient approach. Instead, you can write the image directly to a MemoryStream and obtain the resulting byte array:

using (MemoryStream ms = new MemoryStream())
{
    pdf.SaveAsImage(0, PdfImageType.Bitmap, 300, 300).Save(ms, ImageFormat.Jpeg);
    byte[] imageBytes = ms.ToArray();
}
Enter fullscreen mode Exit fullscreen mode

This approach eliminates the need to create and clean up temporary files, resulting in cleaner code and improved performance. It is especially useful in server-side applications and cloud-based workflows.

Conclusion

With the SaveAsImage() method, Spire.PDF for .NET provides a comprehensive and flexible solution for converting PDF documents to images. From basic single-page exports to batch conversion of multi-page PDFs, from DPI customization to memory-stream output, the library supports a wide range of business requirements.

By combining these techniques according to your application's needs, you can build efficient, reliable, and high-quality PDF-to-JPG conversion workflows while maintaining an optimal balance between image fidelity and system performance.

Top comments (0)