How to convert a PDF document into thumbnail image with specified dimensions in C# and .NET


Sometimes is necessary to convert a page from PDF document into image (png, jpg, bmp, doesn’t really matter to show it in a browser). Let us say, if we’re creating an e-library website. This e-library stores a lot of e-books in a PDF format. And our task is to show cover pages from those PDF books to visitors of our e-library.


Convert a PDF document into thumbnail image with specified dimensions in C# and .NET

Actually a lot of browsers can show a PDF document with help of Acrobat Reader plugin, but this will not work fast when you want to show let’s say cover pages from 50 PDFs. Much faster solution is convert a cover page from each PDF document into image and show it a browser.

Let’s convert a cover page from a PDF into thumbnail PNG image with size of 100 x 100 pixels using C#:

  1. Download the free trial version of the “SautinSoft.PdfFocus.dll” from here: PDF Focus
  2. Create a new project in Visual Studio and add a reference to the “SautinSoft.PdfFocus.dll”.
  3. This is a sample code in C#:

These are code samples in C#:


             string pathToPdf = @"c:\Book.pdf";
    string pathToStoreImages = @"c:\Images\";

    SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
    f.OpenPdf(pathToPdf);

    if (f.PageCount > 0)
    {
        System.Drawing.Image img = null;
        f.ImageOptions.Dpi = 72;
        float thumbnailWidthPx = 100;
        float thumbnailHeightPx = 100;
        int page = 1; //cover page

        //1. Convert PDF into same size image
        img = f.ToDrawingImage(page);
        //Save original image
        img.Save(pathToStoreImages + "Page" + page.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);

        //2. Make thumnail and save it as png
        Bitmap bmpThumnail = new Bitmap((int)thumbnailWidthPx, (int)thumbnailHeightPx);
        Graphics e = Graphics.FromImage(bmpThumnail);
        float koeffW = (float)img.Width / thumbnailWidthPx;
        float koeffH = (float)img.Height / thumbnailHeightPx;
        float maxKoeff = (koeffW > koeffH) ? koeffW : koeffH;
        Rectangle rectSource = new Rectangle(0, 0, img.Width, img.Height);

        int x = 0;
        int y = 0;

        if (koeffW > koeffH)
            y = ((int)thumbnailHeightPx - (int)(img.Height / maxKoeff)) / 2;
        else
            x = ((int)thumbnailWidthPx - (int)(img.Width / maxKoeff)) / 2;

        Rectangle rectDest = new Rectangle(x, y, (int)(img.Width / maxKoeff), (int)(img.Height / maxKoeff));
        e.DrawImage(img, rectDest, rectSource, GraphicsUnit.Pixel);
        bmpThumnail.Save(pathToStoreImages + "Thumbnail" + page.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
    }
                    

Resume: This code uses the SautinSoft’s library – PDF Focus .Net which can convert any types of PDF documents to Word, RTF, Text documents and Images (PNG, JPG, Multipage-TIFF etc).


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:



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.