Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C#

How To Convert PDF to Image Using Ghostscript API

Rate me:
Please Sign up or sign in to vote.
4.89/5 (76 votes)
28 Mar 2010CPOL4 min read 2.1M   45.3K   229  
How to use Ghostscript library to create an image (or images) from a PDF file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace PdfPageTools
{
    /// <summary>
    /// Author: TaGoH
    /// This class provide simple tools to edit a pdf file
    /// </summary>
    public static class PDFTools
    {
        /// <summary>Create a copy of a PDF without security</summary>
        /// <param name="source">The original file</param>
        /// <param name="destination">the destination file</param>
        /// <returns>true if succed</returns>
        static public bool StripSecurity(string source, string destination)
        {
            return StripSecurity(source, destination, null,null,null,null);
        }
        /// <summary>Create a copy of a PDF without security</summary>
        /// <param name="source">The original file</param>
        /// <param name="destination">the destination file</param>
        /// <param name="author">the new Author of this document</param>
        /// <param name="title">the new Title of this document</param>
        /// <param name="Application">the new Application that create this document</param>
        /// <returns>true if succed</returns>
        static public bool StripSecurity(string source, string destination,string author,string title,string application)
        {
            return StripSecurity(source, destination, null,author,title,application);
        }
        /// <summary>Create a copy of a PDF without security</summary>
        /// <param name="source">The original file</param>
        /// <param name="destination">the destination file</param>
        /// <param name="pages">the pages i want to copy</param>
        /// <param name="author">the new Author of this document</param>
        /// <param name="title">the new Title of this document</param>
        /// <param name="Application">the new Application that create this document</param>
        /// <returns>true if succed</returns>
        static public bool StripSecurity(string source, string destination, int[] pages, string author, string title, string application)
        {
            //If a file doesn't exist don't even bother to continue
            if (!System.IO.File.Exists(source)) return false;
            //import document using PDFSharp
            PdfDocument maindoc = PdfReader.Open(source,PdfDocumentOpenMode.Import);
            //Create the Output Document as a new PDF Document
            PdfDocument OutputDoc = new PdfDocument();
            if (author != null) OutputDoc.Info.Author = author;
            if (title != null) OutputDoc.Info.Title = title;
            if (application != null) OutputDoc.Info.Creator = application;
            if ((pages == null) || (pages.Length == 0))
            {
                //Copy over all the pages from original document
                foreach (PdfPage page in maindoc.Pages)
                    OutputDoc.AddPage(page);
            }
            else
            {
                for (int i = 0; i < maindoc.PageCount; i++)
                    if (pages.Contains(i))
                        OutputDoc.AddPage(maindoc.Pages[i]);
            }
            //save new document
            OutputDoc.Save(destination);
            //dispose of objects
            maindoc.Dispose();
            OutputDoc.Dispose();
            return true;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


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

Comments and Discussions