Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert PDF to JPG or PNG using C# or Command Line [closed]

Tags:

I need to convert a PDF file to images. I used for testing purposes "Total PDF Converter" which offers a command line, but it's shareware and I need to find a free alternative.

Does anyone knows such a tool or maybe even a free C# library?

like image 803
martinyyyy Avatar asked Jan 14 '11 20:01

martinyyyy


2 Answers

The convert tool (or magick since version 7) from the ImageMagick bundle can do this (and a whole lot more).

In its simplest form, it's just

convert myfile.pdf myfile.png 

or

magick myfile.pdf myfile.png 
like image 178
Thomas Avatar answered Sep 18 '22 09:09

Thomas


This is an old question, but as a GhostScript answer is missing and there is no hint for multipage PDF export yet I think adding another variant is ok.

gs -dBATCH -dNOPAUSE -sDEVICE=pnggray -r300 -dUseCropBox -sOutputFile=item-%03d.png examples.pdf 

Options description:

  • dBatch and dNOPAUSE just tell gs to run in batch mode, which means more or less it will not ask any questions. Those parameters are also important if you want to run the command in a bash script.
  • sDEVICE tells gs what output format to produce. pnggray is for grayscale, png16m for 24-bit RGB color. If you insist on creating Jpegs use -sDEVICE=jpeg to produce color JPEG files. Use the -dJPEGQ=N (N is an integer from 0 to 100, default 75) parameter to control the Jpgeg quality.
  • -r300 sets the scan resolution to 300dpi. If you prefer a smaller output sizes use -r70 or if you input pdf has a high resoultion use -r600. If you have a PDF with 300dpi and specify -r600 your images will be upscaled.
  • -dUseCropBox tell gs to use a CropBox if defined. A CropBox is specifies an area of interest on a page. If you have a pdf with a large white margin and you don't want this margin on your output this option might help.
  • -sOutputFile defines the name(s) of the output file. The %03d.png part tells gs to include a counter for multiple files. A two page pdf would result in two files named item-001.png and item-002.png.
  • The last (unnamed parameter is the input file.)

Availability: The convert command of imagemagick does use the gs command internally. If you can convert a pdf with imagemagick, you already have gs installed.

Install ghostscript:

RHEL:

yum install ghostscript 

SLES:

zypper install ghostscript 

Debian/Ubuntu:

sudo apt-get install ghostscript 

Windows:

You can find Windows binaries under http://www.ghostscript.com/download/gsdnld.html

like image 36
tobltobs Avatar answered Sep 19 '22 09:09

tobltobs