Click here to Skip to main content
13,454,150 members (52,928 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 28 Feb 2017

Create Thumbnail Image from PDF using Ghostscript

, 1 Mar 2017
Rate this:
Please Sign up or sign in to vote.
Upload PDF, save file name to database, save pdf to a folder, create a thumbnail image of pdf and save it to a folder, and also save the image to database


In this article, I am going to explain how to upload a PDF, save the file name to database and save the PDF file to a folder. Also, create a thumbnail image and save it to a folder and database. This is a very simple way of doing using Ghostscript compared to using Acrobat rasterizing method. It has very few lines of code and is simple to understand.


I am using Ghostscript to rasterize the PDF file to an image by choosing the pagenumber. In this scenario, we need only the first page number to create a thumbnail of it.

Download Ghostscript

Firstly, you need to download Ghostscript DLL from here and click the download button. This contains the DLL files and sample files. Copy the DLL to your project folder and in Visual Studio, add reference to the Ghostscript.NET DLL.

Using the Code

You can perform PDF upload using any standard upload control. Here, I am using Telerik async upload control.

First, get the file name from the controller:

if (RadAsyncUploadFile.UploadedFiles.Count > 0)
   UploadedFile attachment = RadAsyncUploadFile.UploadedFiles[0];
   filename = attachment.FileName;

Then, save this file name to the database by usual SQL query to save.

To save the uploaded pdf file to a folder:

var pdffilename = filename; //or any name you prefer.

var thumbName = somename + "_thumb"; //usually filename _thumb, here I use some name because
                 // here filename will have the .pdf extension. so you need to perform
                 //string function to remove extension or use Path.GetFileNameWithoutExtension(path)

string pathfolder = (System.Configuration.ConfigurationManager.AppSettings["D:\ProjectName\"]);

UploadedFile attachment = RadAsyncUploadFile.UploadedFiles[0];

attachment.SaveAs(pathfolder + pdffilename, true);

Now, create a new class file called Filemanagement and create an object for it in this codebehind:

FileManagement _fileManagement = new FileManagement();
 var id =  sql query to get the id from the table where we saved the filename;

 string filepath = pathfolder + pdffilename;
  _fileManagement.CreateThumbnail(filepath, thumbName, currMac.ArtworkId);

Now in the class file, add the following code:

using System;
using System.Linq;
using Project.DataAccess.Interfaces;
using System.IO;
using System.Drawing;
using Ghostscript.NET.Rasterizer;
using Project.Entities.ProjectEntity;

namespace Project.DataAccess
    public interface ISample
        void Start(string filePath, string thumbName, long id);

    public class RasterizerCropSample : ISample
        readonly ProjectEntity_context = new ProjectEntity();

        public void Start(string filePath, string thumbName, long artworkId)
            int desired_x_dpi = 10;
            int desired_y_dpi = 10;

            string inputPdfPath = filePath;
            string outputPath = @"D:\Project\Thumbnails\";

            using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
                rasterizer.CustomSwitches.Add("[/CropBox [24 72 559 794] /PAGES pdfmark");


                var pageNumber = 1;               

               // string pageFilePath = Path.Combine(outputPath, thumbName + ".png");

                Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
                //to save in folder
                img.Save(pageFilePath, ImageFormat.Png);
                //to save in database
                byte[] byteImage = ImageToByteArray(img);
                var getArtwork = 
                  (from m in _context.tArtworkFiles where m.ArtworkId == idselect m).FirstOrDefault();
                if (getArtwork != null)
                    getArtwork.Thumbnail = byteImage;
        public byte[] ImageToByteArray(System.Drawing.Image imageIn)
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();

    public class FileManagement : IFileManager
        readonly ProjectEntity_context = new ProjectEntity();
        public void CreateThumbnail(string filepath, string thumbName, long id)
            RasterizerCropSample thumbnailCreate = new RasterizerCropSample();
            thumbnailCreate.Start(filepath, thumbName, id);          

Here, we have included the ghostscript to rasterize the PDF by selecting the firstpage and creating an Image.

We then save the image to a folder.

We convert the rasterized image to a ByteArray and then save it to a database column of datatype as Image.

Hope this helps!

Any queries, please leave a comment.

Points of Interest

I tried using Acrobat method which is very complicated and used too many lines of code.

But here, I use Ghostscript which made the whole process simple and easy to code.


  • 28/02/2017: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Saranya Mahesh
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180321.1 | Last Updated 1 Mar 2017
Article Copyright 2017 by Saranya Mahesh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid