Convert PDF Pages to JPG Images in C#

Do you need to render the pages in a PDF file to JPG images in C#? If yes, this article provides one of the best and easiest methods to convert pages in a PDF to JPG in C#. Not only this, the method ensures high-quality and speedy PDF to JPG conversion. Let’s go through a complete step-by-step guide on how to convert PDF to JPG in C#.

C# Library to Convert PDF to JPG

To convert PDF files to JPG images, we will use Aspose.PDF for .NET. It is a powerful PDF manipulation library that lets you create and process PDF files from within your .NET applications. Furthermore, it allows you to convert PDF documents to other formats seamlessly.

You can either download the library or install it using NuGet.

PM> Install-Package Aspose.PDF

How to Convert PDF to JPG in C#

The following are the steps to convert the pages in a PDF to JPG images in C#.

The following code sample shows how to convert a PDF to JPG using C#.

// Open PDF document
Document pdfDocument = new Document("Document.pdf");
// Loop through pages
foreach (var page in pdfDocument.Pages)
{
// Create file stream
using (FileStream imageStream = new FileStream(string.Format("page_{0}.jpg", page.Number), FileMode.Create))
{
// Create resolution object
Resolution resolution = new Resolution(300);
// Create Jpeg device with specified attributes
// Width, Height, Resolution
JpegDevice JpgDevice = new JpegDevice(500, 700, resolution);
// Convert a particular page and save the image to stream
JpgDevice.Process(page, imageStream);
// Close stream
imageStream.Close();
}
}
view raw pdf-to-jpg.cs hosted with ❤ by GitHub

Convert a Page of PDF to JPG in C#

You can also convert only a single page of PDF to JPG. In that case, you can access the desired page from Document.Pages collection. The following are the steps to convert only a single page of PDF to JPG.

The following code sample shows how to convert a single page in PDF to JPG.

// Open PDF document
Document pdfDocument = new Document("Document.pdf");
// Set page number
int pageNumber = 1;
// Create FileStream for the output image
using (FileStream imageStream = new FileStream(string.Format("page_{0}.jpg", pageNumber), FileMode.Create))
{
// Create Resolution object
Resolution resolution = new Resolution(300);
// Create Jpeg device with specified attributes
// Width, Height, Resolution
JpegDevice JpgDevice = new JpegDevice(500, 700, resolution);
// Convert a particular page and save the image to stream
JpgDevice.Process(pdfDocument.Pages[pageNumber], imageStream);
// Close stream
imageStream.Close();
}

Free C# PDF to JPG Conversion

You can get a free temporary license and convert as many PDF files to JPG as you want without any limitations.

Explore C# PDF to JPG Converter

You can explore more about the C# PDF library using the documentation. In case you would have any questions or queries, you can contact us via our forum.

Conclusion

In this post, you have learned how to convert pages in a PDF file to JPG images using C#. The code samples have shown how to convert all or desired pages of PDF to JPG images. You only need to install the library and you can carry out PDF to JPG conversion in your C# application.

See Also