Convert PDF to JPG or PNG using C# or Command Line
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
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
anddNOPAUSE
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
Question is quite old, but recently I have found this solution which worked for me: https://github.com/jhabjan/Ghostscript.NET. It is also available as an nuget download.
Here is the sample code for converting all pdf pages into png images:
private static void Test()
{
var localGhostscriptDll = Path.Combine(Environment.CurrentDirectory, "gsdll64.dll");
var localDllInfo = new GhostscriptVersionInfo(localGhostscriptDll);
int desired_x_dpi = 96;
int desired_y_dpi = 96;
string inputPdfPath = "test.pdf";
string outputPath = Environment.CurrentDirectory;
GhostscriptRasterizer _rasterizer = new GhostscriptRasterizer();
_rasterizer.Open(inputPdfPath, localDllInfo, false);
for (int pageNumber = 1; pageNumber <= _rasterizer.PageCount; pageNumber++)
{
string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
Image img = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
img.Save(pageFilePath, ImageFormat.Png);
}
_rasterizer.Close();
}