This project has moved and is read-only. For the latest updates, please go here.

Convert PDF to a set of PJPEG files

Jul 23, 2013 at 7:57 AM
Hi, thank you for such nice library!
I've read examples and now trying to convert PDF to a set of progressive JPG files, but i didn't succeed.
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(144, 144);

settings.ColorSpace = ColorSpace.sRGB;

using (MagickImage coverPage = new MagickImage())
{
    coverPage.Format = MagickFormat.Pjpeg;

    coverPage.Quality = 80;

    coverPage.Read(fileName + "[0]", settings);
    
    coverPage.Write(WritePath);
}
Image is not saved as PJPEG. It works only when i add PJPEG: before filepath.
string WritePath = String.Format("PJPEG:{0}.jpg", OutPath);
I can go this way, but there is other issue: i want to have output images of fixed size (max height ~500px)

when i try to call
coverPage.Resize(0, 500);
before saving image, it sized but have a black background:

Image

what am i doing wrong? what is the best way to solve this?

Regards,
Denis
Jul 23, 2013 at 8:07 AM
The black pixels are probably transparent pixels. You can give them a color using this method:
coverPage.ColorAlpha(new MagickColor("#fff"));
Jul 23, 2013 at 8:40 AM
Tried this, no chnages.

Here is Sample PDF

after converting i've got this Image (no black background, but image not resized)

while using resize i have black background:

Image
Jul 23, 2013 at 8:51 AM
Solved this via ImageMagick in commandline:
convert.exe -colorspace sRGB -interlace line -density 144x144 -quality 100 -resize 500x500 -background white -alpha remove d:\test.pdf d:\test.jpg
how can i execute the same using Magick.Net: -background white -alpha remove ?
Jul 23, 2013 at 9:59 AM
Edited Jul 23, 2013 at 11:27 AM
Very interesting, everything okay when i first save PDF page as image, reopen it and perform needed actions:
MagickReadSettings settings = new MagickReadSettings();

settings.Density = new MagickGeometry(144, 144);

// 1 step - read PDF first page, and save it as JPEG image

using (MagickImage coverPage = new MagickImage())
{
    coverPage.Read(fileName + "[0]", settings);

    coverPage.Write(WritePath);
}

// 2 step - reopen just created file, resize and overwrite

using (MagickImage image = new MagickImage(WritePath))
{
    image.Format = MagickFormat.Pjpeg;

    if (image.Width < image.Height)
        image.Resize(0, 500);
    else
        image.Resize(500, 0);

    using (FileStream fs = new FileStream(WritePath, FileMode.Create))
    {
        image.Write(fs);
    }
}
anyway, thanks for such good library!
Jul 23, 2013 at 12:11 PM
You should not write an jpeg and then open it to modify it, you will lose image information. With the following example I am able to create the page with a white background:
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(144, 144);
settings.ColorSpace = ColorSpace.sRGB;

using (MagickImageCollection images = new MagickImageCollection())
{
  images.Read("29_6a Farmout Flyer April 2012.pdf", settings);
  
  MagickImage coverPage = images.First();
  coverPage.Resize(0, 500);
  coverPage.ColorAlpha(new MagickColor("#fff"));
  coverPage.Quality = 80;
  coverPage.Write("PJPEG:29_6a Farmout Flyer April 2012.jpg");
}
Jul 23, 2013 at 12:24 PM
Edited Jul 23, 2013 at 12:26 PM
Tried your solution, but result image is not PJPEG.
MagickReadSettings settings = new MagickReadSettings();

settings.Density = new MagickGeometry(144, 144);

settings.ColorSpace = ColorSpace.sRGB;

using (MagickImageCollection images = new MagickImageCollection())
{
    images.Read(fileName + "[0]", settings);

    MagickImage coverPage = images.First();
    coverPage.Format = MagickFormat.Pjpeg;
    coverPage.Resize(0, 500);
    coverPage.ColorAlpha(new MagickColor("#fff"));
    coverPage.Quality = 80;
    coverPage.Write(WritePath);
}
i'm using http://regex.info/exif.cgi to read EXIF info
Jul 23, 2013 at 12:56 PM
Edited Jul 23, 2013 at 5:28 PM
Irfanview (yes I still need to make MagickViewer) is reporting this image as a progressive jpeg: JPEG, progressive, quality: 80.

And I don't get a exif profile from the image, profile is null in the following example.
using (MagickImage image = new MagickImage("29_6a Farmout Flyer April 2012.jpg"))
{
   ExifProfile profile = image.GetExifProfile();                    
}
Aug 30, 2013 at 8:41 AM
Hi ,
I have a similar problem, I'm trying to create a thumbnail from the first page of a pdf.
I copy and run the example by dlemstra and it's ok , but with this pdf i get a black backgound
What can i do?
Aug 31, 2013 at 9:40 PM
With the following code I am getting a white background.
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(300, 300);
settings.FrameIndex = 1;

using (MagickImage image = new MagickImage())
{
    image.Read("Touchline_267700.pdf", settings);
    image.Write("Touchline_267700.FirstPage.jpg");
}
Sep 2, 2013 at 7:41 AM

I'm sorry you are right I try to add the border without compose so i achieve a the black background.
thanks
emanuele.

Apr 17, 2014 at 7:57 PM
Edited Apr 17, 2014 at 7:59 PM
The way to remove the background is to use the Alpha method, which allows you to passing a settings enum value. Adding the following to a batch process I was running did the trick:
img.BackgroundColor = new MagickColor(Color.White);
img.Alpha(AlphaOption.Remove);
This seems equivalent to what the op was doing in the command line:
convert.exe -colorspace sRGB -interlace line -density 144x144 -quality 100 -resize 500x500 -background white -alpha remove d:\test.pdf d:\test.jpg