Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create Thumbnail Image from PDF using Ghostscript

0.00/5 (No votes)
1 Mar 2017 2  
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

Introduction

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.

Background

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("-dUseCropBox");
                rasterizer.CustomSwitches.Add("-c");
                rasterizer.CustomSwitches.Add("[/CropBox [24 72 559 794] /PAGES pdfmark");
                rasterizer.CustomSwitches.Add("-f");

                rasterizer.Open(inputPdfPath);

                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;
                    _context.SaveChanges();
                }               
            }
        }
        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();
        [STAThread]
        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.

History

  • 28/02/2017: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here