How to convert a PDF document into thumbnail image with specified dimensions in C# and VB.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 VB.Net
Convert a PDF document into thumbnail image with specified dimensions in C# and VB.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: http://www.sautinsoft.com/products/pdf-focus/index.php
  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#:
    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).

To launch this code, please download the free trial version of the “SautinSoft.PdfFocus.dll” from here:

http://www.sautinsoft.com/products/pdf-focus/index.php.

Or here: http://www.sautinsoft.com/components/pdf_focus_net.zip.

Need any assistance? Ask us online at http://www.sautinsoft.com.

Max Sautin

Share on Facebook

Leave a Comment

Your email address will not be published.