How to convert PDF to JPG image with high Quality in C# and .Net

In this article I’d like to tell how to convert PDF to JPG in .Net platform with various Jpeg Quality (High and Low). In addition I’ll show you a simple C# sample which help us to understand how the size of .jpg file depends of jpeg quality.

As you may know that JPEG format is very useful when you are working with photos and images containing various gradients. Because this format allows to reduce the size (weight) of image up to 10 times and keep a reasonable loss of quality. Sometimes the quality loss will be barely distinguishable to the human eye. Without belaboring the point, let’s start:

For demonstration purposes we’ll operate with PDF document named “parkmap.pdf”. It’s a PDF with graphical map of the Singapore zoo. It has 1 page and “weighs” 1372 Kb.

parkmap.pdf, 1372 Kb.
parkmap.pdf, 1372 Kb.

Our task is convert this PDF document into JPG format with high quality, but keep a reasonable weight of .jpg file.

The whole conversion process will be operated using “SautinSoft.PdfFocus” library. Let’s start:

  1. Download the latest PDF Focus .Net library.
  2. Open Visual Studio and create new C# Console Application, named “ZooConverter”.
  3. Using the “Solution Explorer” add a reference to the “SautinSoft.PdfFocus.dll”. You may find this .dll file inside .zip package which you downloaded in step 1. In our case it located inside “Bin\Net 4.0” subfolder of .zip.
  4. Let’s type C# code to convert this PDF to JPG with 95 level of quality:
        SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
     
        string pdfPath = @"D:\Work\parkmap.pdf";
        string jpegDir = Path.GetDirectoryName(pdfPath);
     
        f.OpenPdf(pdfPath);
     
        if (f.PageCount > 0)
        {
            f.ImageOptions.ImageFormat = ImageFormat.Jpeg;
            f.ImageOptions.Dpi = 120;
            // Let's set the quality to 95.
            f.ImageOptions.JpegQuality = 95;
     
            for (int page = 1; page <= f.PageCount; page++)
            {
                string jpegPath = Path.Combine(jpegDir, String.Format("page{0}.jpg", page));
                f.ToImage(jpegPath, page);
            }
        }

After launching the code we’ll get the file:
page1.jpg, 1748×1982, 1318 Kbytes. That seems to be the case, that quality of the produced image is the same as original PDF. We also don’t see any big differences sizes of files: PDF (1372 Kb) and Jpeg (1318 Kb).

Page1.jpg, 95 level of quality, 1318 Kb.
Page1.jpg, 95 level of quality, 1318 Kb.
  1. Let’s try to reduce the size of JPEG and leave the quality acceptable for human eye. Let’s change the JpegQuality and launch the code again:
        // Let's set the quality to 50.
        f.ImageOptions.JpegQuality = 50;
Page1.jpg, 50 level of quality, 440 Kb.
Page1.jpg, 50 level of quality, 440 Kb.

Wow! With help of JpegQuality we’ve reduced the file in 3 times: page1.jpg, 1748×1982, 404 Kbytes. At first sight, the image looks similar. But if you zoom it to 300% you will see some graphical noises and loss of quality.

  1. To catch a golden mean between size and quality, let’s try set the JpegQuality to 80:
        // Let's set the quality to 80.
        f.ImageOptions.JpegQuality = 80;
Page1.jpg, 80 level of quality, 699 Kb.
Page1.jpg, 80 level of quality, 699 Kb.

Now we our JPG file looks the same as original PDF, but it “weighs” only ~700 Kb.

See a comparative table: zoom 300%, various levels of JpegQuality:

Original PDF, zoom 300%, Total size 1372 Kb.
Original PDF, zoom 300%, Total size 1372 Kb.
JPG with 10 quality, zoom 300%, Total size 162 Kb.
JPG with 10 quality, zoom 300%, Total size 162 Kb.

JPG with 25 quality, zoom 300%, Total size 297 Kb.
JPG with 25 quality, zoom 300%, Total size 297 Kb.

JPG with 50 quality, zoom 300%, Total size 440 Kb.
JPG with 50 quality, zoom 300%, Total size 440 Kb.

JPG with 80 quality, zoom 300%, Total size 699 Kb.
JPG with 80 quality, zoom 300%, Total size 699 Kb.

JPG with 95 quality, zoom 300%, Total size 1318 Kb.
JPG with 95 quality, zoom 300%, Total size 1318 Kb.

JPG with 100 quality, zoom 300%, Total size 2578 Kb.
JPG with 100 quality, zoom 300%, Total size 2578 Kb.

Summarize, I can say that JpegQuality sometimes may be useful and you can save a  tons of megabytes or gigabytes when you operate with thousands documents.

Share on Facebook

Leave a Comment

Your email address will not be published.