32

Looking at other posts for this could not find an adequate solution that for my needs. Trying to just get the first page of a pdf document as a thumbnail. This is to be run as a server application so would not want to write out a pdf document to file to then call a third application that reads the pdf to generate the image on disk.

doc = new PDFdocument("some.pdf");
page = doc.page(1);
Image image = page.image;

Thanks.

AndrewB
  • 962
  • 1
  • 10
  • 18
  • used product from glyph and Cog – AndrewB Aug 28 '10 at 23:02
  • My solution using Ghostscript.NET & Ghostscript used for ASP.NET Core 1.0 Project - http://stackoverflow.com/a/40598893/707162 – SamJackSon Nov 14 '16 at 22:28
  • FreeSpire.pdf can do this, and does not require GhostScript or anything else that charges massive fees for commercial license: https://www.e-iceblue.com/Introduce/free-pdf-component.html#.XDXe-Vz7SUk – soupy1976 Jan 09 '19 at 12:30

5 Answers5

30

Matthew Ephraim released an open source wrapper for Ghostscript that sounds like it does what you want and is in C#.

Link to Source Code: https://github.com/mephraim/ghostscriptsharp

Link to Blog Posting: http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/

You can make a simple call to the GeneratePageThumb method to generate a thumbnail (or use GeneratePageThumbs with a start and end page number to generate thumbnails for multiple seperate pages, with each page being a seperate output file), default file format is jpeg but you can change it, and many other options, by using the alternate GenerateOutput method call and specify options such as file format, page size, etc...

Peter
  • 9,643
  • 6
  • 61
  • 108
  • 14
    Please note that GhostScript itself is only open source for non-profit / open source projects. For enterprise and commerical apps you have to pay an astronomical licence fee... – doglobster Jul 19 '13 at 11:10
  • Requires ***GhostScript*** installation ? – Kiquenet Jan 01 '17 at 11:19
  • Doesn't work with IIS 64bit – Hp93 Jun 22 '17 at 03:12
  • @Hp93 At the time this library was written I'm guessing the author only wrote the library using the x86 version of GhostScript. You could try installing the x64 version and recompiling his library to x64 from source. – Peter Jun 23 '17 at 00:00
  • Readers - Also see this related SO post with 32/64 bit GhostScript solution. https://stackoverflow.com/a/23262506/943435 – Yogi Feb 09 '18 at 18:18
  • GeneratePageThumbs is throwing error and GeneratePageThumb working fine. When I dig into this GeneratePageThumb is actually calling GeneratePageThumbs internally. So GeneratePageThumbs expect outputPath which is a string and output files can be multiple. – Aamir Nakhwa Jan 20 '20 at 10:58
21

I think that Windows API Code pack for Microsoft .NET framework might do the trick easiest. What it can is to generate the same thumbnail that Windows Explorer does (and that is first page), and you can chose several sizes, they go up to 1024x1024, so it should be enough. It is quite simple, just create ShellObject.FromParsingName(filepath) and find its Thumbnail subclass.

The problem might be what your server is. This works on Windows 7, Windows Vista and I guess Windows Server 2008. Also, Windows Explorer must be able to show thumbnails on that machine. The easiest way to insure that is to install Adobe Reader. If all of this is not a problem, I think that this is the most elegant way.

UPDATE: Adobe Reader has dropped support for thumbnails in the recent versions so its legacy versions must be used.

UPDATE2: According to comment from Roberto, you can still use latest version of Adobe Reader if you turn on thumbnails option in Edit - Preferences - General.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • Just to add that it is free and that its license is liberal (including that you can download and change source code). – Ivan Ičin Jan 16 '11 at 20:03
  • Yes, this is the best solution for commerical products. Peter's accepted answer is great for open source projects, but you have to have a GhostScript licence to use it in for enterprise / commercial products. (We were quoted $10K one-off fee plus $50k annually!) – doglobster Jul 19 '13 at 11:09
  • Whoa! Might as well hire a full time developer and have him build new software. This should be the marked answer. – strider Feb 28 '14 at 22:53
  • 1
    Update: I've used Adobe Reader version 2018 and Windows API to create thumbnails. Works great. However, you need to enable thumbnails option in Reader (menu Edit - Preferences - General). On an IIS web server the thumbnail option may need to be enabled on the AppPool account (I did not try this). – Yogi Mar 01 '18 at 15:12
3

Download PDFLibNet and use the following code

public void ConvertPDFtoJPG(string filename, String dirOut)
{
    PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
    _pdfDoc.LoadPDF(filename);

    for (int i = 0; i < _pdfDoc.PageCount; i++)
    {

        Image img = RenderPage(_pdfDoc, i);

        img.Save(Path.Combine(dirOut, string.Format("{0}{1}.jpg", i,DateTime.Now.ToString("mmss"))));

    }
    _pdfDoc.Dispose();
    return;
}
public  Image RenderPage(PDFLibNet.PDFWrapper doc, int page)
{
    doc.CurrentPage = page + 1;
    doc.CurrentX = 0;
    doc.CurrentY = 0;

    doc.RenderPage(IntPtr.Zero);

        // create an image to draw the page into
        var buffer = new Bitmap(doc.PageWidth, doc.PageHeight);
        doc.ClientBounds = new Rectangle(0, 0, doc.PageWidth, doc.PageHeight);
        using (var g = Graphics.FromImage(buffer))
        {
            var hdc = g.GetHdc();
            try
            {
                doc.DrawPageHDC(hdc);
            }
            finally
            {
                g.ReleaseHdc();
            }
        }
        return buffer;

}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

I used to do this kind of stuff with imagemagick (Convert) long ago. There is a .Net Wrapper for that, maybe it's worth checking out : http://imagemagick.codeplex.com/releases/view/30302

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60
0

http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx

This works very well. The only dependencies are GhostScript's gsdll32.dll (you need to download GhostScript separately to get this, but there is no need to have GhostScript installed in your production environment), and PDFSharp.dll which is included in the project.

saille
  • 9,014
  • 5
  • 45
  • 57