Convert PDF to JPG with high Quality in C# and .NET


In today's digital world, working with various document formats has become an integral part of IT development. One such popular task is converting PDF files to images, such as JPG, for easy viewing, publishing, or automated data processing. In this context, using libraries that enable these operations with high quality and minimal effort is a key development aspect. Using JPG as a format provides a balance of quality and file size, making it suitable for most display and storage tasks.

One such tool is PDF Focus .NET component of the powerful SautinSoft SDK, a universal library for processing PDF documents on the .NET platform. It is characterized by its simplicity, high performance, and extensive document management capabilities. In this article, we will discuss how to use it to convert PDF to JPG with high quality in C# and .NET, and answer questions regarding the advantages and applications of this approach.

Converting PDF to an image opens up a wide range of possibilities:

  • Quick View: For quickly displaying PDF content without the need for a full-fledged PDF viewer.
  • Web Publishing: Embedding images on websites or blogs, as images are typically easier to display and load.
  • Archiving and protection: Converting PDF pages to images prevents text editing.
  • Automation: Image analysis using OCR or computer vision.

What is the benefit of this approach?

  • Quality control: You can select the optimal parameters for a balance between quality and file size.
  • Automation: Can be embedded in scripts or server-side solutions to automatically process large volumes of PDF documents.
  • Flexibility: Easily select specific pages or save the entire document in sections.
  • High quality: The settings allow you to produce images with sharp details, which is important when using images in design projects or for OCR.

This code can be easily adapted for real-world automation tasks or embedded into a larger application. It demonstrates how to manage image quality, which is important when preserving detail and clarity.

Complete code

using System;
using System.IO;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Before starting, we recommend to get a free key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // SautinSoft.PdfFocus.SetLicense("...");

            // Convert PDF to JPG with high Quality
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();

            string pdfFile = Path.GetFullPath(@"..\..\..\Potato Beetle.pdf");
            string jpegDir = new DirectoryInfo(Directory.GetCurrentDirectory()).CreateSubdirectory("Result").FullName;

            f.OpenPdf(pdfFile);

            if (f.PageCount > 0)
            {
                // Set image properties: Jpeg, 300 dpi
                f.ImageOptions.ImageFormat = SautinSoft.PdfFocus.CImageOptions.ImageFormats.Jpeg;
                f.ImageOptions.Dpi = 300;

                // Set 95 as JPEG quality
                f.ImageOptions.JpegQuality = 95;

                //Save all PDF pages to image folder, each file will have name Page 1.jpg, Page 2.jpg, Page N.jpg
                for (int page = 1; page <= f.PageCount; page++)
                {
                    string jpegFile = Path.Combine(jpegDir, String.Format("Page {0}.jpg", page));

                    // 0 - converted successfully                
                    // 2 - can't create output file, check the output path
                    // 3 - conversion failed
                    f.ImageOptions.PageIndex = page - 1;
                    int result = f.ToImage(jpegFile);

                    // Show only 1st page
                    if (page == 1 && result == 0)
                        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(jpegFile) { UseShellExecute = true });
                }
            }
        }
    }
}

Download

Imports System
Imports System.IO

Namespace Sample
	Friend Class Sample
		Shared Sub Main(ByVal args() As String)
			' Before starting, we recommend to get a free key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' SautinSoft.PdfFocus.SetLicense("...");

			' Convert PDF to JPG with high Quality
			Dim f As New SautinSoft.PdfFocus()

			Dim pdfFile As String = Path.GetFullPath("..\..\..\Potato Beetle.pdf")
			Dim jpegDir As String = (New DirectoryInfo(Directory.GetCurrentDirectory())).CreateSubdirectory("Result").FullName

			f.OpenPdf(pdfFile)

			If f.PageCount > 0 Then
				' Set image properties: Jpeg, 300 dpi
				f.ImageOptions.ImageFormat = SautinSoft.PdfFocus.CImageOptions.ImageFormats.Jpeg
				f.ImageOptions.Dpi = 300

				' Set 95 as JPEG quality
				f.ImageOptions.JpegQuality = 95

				'Save all PDF pages to image folder, each file will have name Page 1.jpg, Page 2.jpg, Page N.jpg
				For page As Integer = 1 To f.PageCount
					Dim jpegFile As String = Path.Combine(jpegDir, String.Format("Page {0}.jpg", page))

					' 0 - converted successfully                
					' 2 - can't create output file, check the output path
					' 3 - conversion failed
					f.ImageOptions.PageIndex = page - 1
					Dim result As Integer = f.ToImage(jpegFile)

					' Show only 1st page
					If page = 1 AndAlso result = 0 Then
						System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(jpegFile) With {.UseShellExecute = True})
					End If
				Next page
			End If
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.