Mark
Mark

Reputation: 314

How to Convert PDF File into Image File using ImageMagick

How should I convert pdf file into image (.jpg, .gif etc) using ImageMagick on c# programming platform? or is there any third party library aside from ImageMagick that can be used to do this?

Upvotes: 0

Views: 2139

Answers (1)

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90305

Ghostscript can read PDF (as well as PostScript and EPS) and convert it to many different image formats.

*BTW, ImageMagick cannot do that itself -- ImageMagick also utilizes Ghostscript for exactly that conversion as an external 'delegate'. ImageMagick is great for continuing to process and manipulate image files -- at which jobs it really excels!

The command gs -h (or on Windows: gswin32c.exe -h) should give you an overview about the different devices that are built into your Ghostscript:

GPL Ghostscript GIT PRERELEASE 9.05 (2011-03-30)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
Usage: gs [switches] [file1.ps file2.ps ...]
Most frequently used switches: (you can use # in place of =)
 -dNOPAUSE           no pause after page   | -q       `quiet', fewer messages
 -g<width>x<height>  page size in pixels   | -r<res>  pixels/inch resolution
 -sDEVICE=<devname>  select device         | -dBATCH  exit after last file
 -sOutputFile=<file> select output file: - for stdout, |command for pipe,
                                         embed %d or %ld for page #
Input formats: PostScript PostScriptLevel1 PostScriptLevel2 PostScriptLevel3 PDF
Default output device: x11alpha
Available devices:
   alc1900 [....] bmp16 bmp16m [...]
   bmp256 bmp32b bmpgray bmpmono bmpsep1 bmpsep8 [....] jpeg jpegcmyk jpeggray 
   pamcmyk32 pamcmyk4 pbm pbmraw pcl3 pcx16 pcx24b [....]
   pcx256 pcx2up pcxcmyk pcxgray pcxmono pdfwrite pgm pgmraw pgnm pgnmraw
   png16 png16m png256 png48 pngalpha
   pnggray pngmono pnm pnmraw ppm ppmraw [....] tiff12nc tiff24nc tiff32nc tiff48nc
   tiff64nc tiffcrle tiffg3 tiffg32d tiffg4 tiffgray tifflzw tiffpack
   tiffscaled tiffscaled24 tiffscaled8 tiffsep tiffsep1 [....]

So, to create a series of PNGs from the multipage PDF my_pdf.pdf with a certain image size (I chose DIN A4 paper format at 72 dpi) and resolution, use the pngalpha device. Try this command:

 gswin32c ^
    -o my_pdf_page_%03d.png ^
    -sDEVICE=pngalpha ^
    -dPDFFitPage ^
    -g595x842 ^
    -r72x72 ^
     my_pdf.pdf

Upvotes: 1

Related Questions