Export PDF to JPG(s) in C#

25,978

Solution 1

Take a look at Ghostscript. You can render PDF to images with it.

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

Solution 2

Ghostscript is currently the de-facto standard for rendering PDFs. It's a bit tricky to wrap, even using GhostScriptSharp.

Jason Morse wrote a great C# wrapper for rendering PDFs as a plugin to the open-source imageresizing.net library.

If it's an asp.net application, the library allows on-the-fly rendering, so you can just add a querystring to get the jpeg/png version:

/pdfs/letter.pdf?format=jpg&page=2

You can also use the managed API instead (in any application type - not asp.net specific)

ImageBuilder.Current.Build("letter.pdf","dest.jpg",new ResizeSettings("format=jpg;page=2"));

The PdfRenderer plugin is GPL licensed, just like Ghostscript.

Solution 3

ABCpdf exports PDF documents to JPEG with C#. See: http://www.websupergoo.com/helppdfnet/source/4-examples/19-rendering.htm

Solution 4

(disclaimer: I work for Atalasoft and wrote a lot of the PDF technology) If you use the PdfDecoder in Atalasoft dotImage, this is straight forward:

public void PdfToJpegThumb(Stream srcStream, int pageNo, int maxDimension, Stream dstStream)
{
    PdfDecoder decoder = new PdfDecoder();
    decoder.Resolution = 96; // reduce default resolution to speed up rendering
    // render page
    using (AtalaImage pdfimage = decoder.read(srcStream, pageNo, null)) {
        Thumbnail tn = new Thumbnail(maxDimension, maxDimension);
        // make a thumbnail image
        using (AtalaImage tnImage = tn.Create(pdfImage)) {
            // save it
            tnImage.Save(dstStream, new JpegEncoder(), null);
        }
    }
}
Share:
25,978
Jason
Author by

Jason

http://js1k.com/2012-love/shim.html

Updated on July 09, 2022

Comments

  • Jason
    Jason almost 2 years

    I need to save a one page pdf document as an image for a thumbnail on a website.

    I've been messing around with PDFSharp and have had no luck.

    I have tried this: http://www.pdfsharp.net/wiki/ExportImages-sample.ashx?AspxAutoDetectCookieSupport=1 but all it does is extract the embedded images in the PDF file which is not the desired result.

    Ideas on how to do this? Anyone know a good library that can handle this?

    Edit: Please let me know why this is such a bad question. If anyone has a good solution to this it would be a great resource for many other people. Especially since google searches come up empty.

  • Jason
    Jason over 12 years
    Works like a champ. I recommend getting the source. It is easier to follow and it is also a bit different that the example on the blog. The only thing is you have to know the desired width/height. I guess I'll work on figuring out where to glean that information from.
  • Jason
    Jason over 12 years
    I'm not crazy about this solution, other than the fact that it works. If anyone has a better solution let us know and I'll give you the checkmark.
  • Jason
    Jason over 12 years
    That'd be pretty neat if it didn't cost 2000+ bucks. =P
  • Jason
    Jason over 12 years
    I will take a look at this and see if its cleaner than using Ghostscript(It has to be). Thanks!
  • Jason
    Jason over 12 years
    Its taken from mattephraim.com/blog/2009/01/06/…. Also, you are missing all the helper methods like 'GetArgs' etc
  • Jason
    Jason over 12 years
    I'll check it out today.
  • CompanyDroneFromSector7G
    CompanyDroneFromSector7G over 9 years
    Thanks for the correction
  • David Freire
    David Freire about 9 years
    what was your solution for getting the page size on the pdf?