Search code examples
c#imagemagick-convertgraphicsmagickmagick.net

Convert first page to tif using Magick.NET


Following gm convert command converts first page of source.pdf to output.tif

convert source.pdf[0] output.tif

I wonder how to do it with Magick.NET library? Following code does not work for me.

using (MagickImage image = new MagickImage("source.pdf"))
{
  image.Write("output.tif");
}

Solution

  • ImageMagick cannot handle PostScript and PDF files itself and by its own, for this it uses a third party software called Ghostscript.

    So, you need to install the latest version of GhostScript before you can convert a pdf using Magick.NET.

    After installing GhostScript use following code to extract first page to TIF-file.

            using (MagickImageCollection image = new MagickImageCollection())
            {
                MagickReadSettings settings = new MagickReadSettings();
                settings.Density = new Density(300, 300); // Settings the density to 300 dpi will create an image with a better quality
                settings.FrameIndex = 0; // First page
                settings.FrameCount = 1; // Number of pages
                image.Read(@"source.pdf", settings);
                image.Write(@"output.tif");
            }
    

    You can adjust quality of resulting TIF by changing settings.Density param (300 dpi is for high quality offset/digital printing, 72 dpi is ok for monitor screens only).