Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#
Tip/Trick

Create Thumbnail Image from PDF using Ghostscript

Rate me:
Please Sign up or sign in to vote.
3.50/5 (8 votes)
1 Mar 2017CPOL2 min read 16.7K   12   1
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:

C#
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:

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIncomplete Code Example Pin
Member 132692526-Jan-21 1:50
Member 132692526-Jan-21 1:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.