append images to output pdf file

May 6, 2014 at 4:01 PM
Hi, all!

first sorry my English.

Magick.NET is great!
but I have a little issue.
this is not an issue with Magick.NET, but i can't ask it in usual programers forum, because it directly relates to Magick.NET and I hope to get help here.
here is a problem:
there is 300 jpg files. size is about 500 Kb each. I need to create one pdf from these 300 jpg. I used an example from documentation - Create a PDF from two images - and it works fine! but if there 300 images, program starts to eat 1,5 Gb of memory. this is not acceptable for me. I find another way to do this and here is my code:
            using (MagickImageCollection collection = new MagickImageCollection())
            {
                string[] jpgs = Directory.GetFiles(@"c:\PDF\", @"*.jpg");
               
                using (FileStream fs = new FileStream(@"c:\PDF\out.pdf", FileMode.Append))
                {
                    foreach (string jpg in jpgs)
                    {
                        MagickImage img = new MagickImage(jpg);
                        img.Format = MagickFormat.Pdf;
                        collection.Add(img);
                        collection.Write(fs);
                        fs.Flush();
                        collection.Clear();//if don't do this, memory will grow very fast
                    }
                }
            }
Output pdf file have a size by which it can be argued that there is all of images! but if I open it - there is only one page! the last added page! all I need - to see all of the added pages.

what am I doing wrong?

thank you very much!
Coordinator
May 6, 2014 at 8:47 PM
Edited May 6, 2014 at 8:48 PM
If you want to create a PDF with 300 images you will first have to read all of them in memory. You could use the Q8 instead of the Q16 version of Magick.NET to reduce the memory that is being used.

You cannot write a multipage PDF file by appending to the file. You will have to write the whole file at once:
using (MagickImageCollection collection = new MagickImageCollection())
{
  string[] jpgs = Directory.GetFiles(@"c:\PDF\", @"*.jpg");

  foreach (string jpg in jpgs)
  {               
    MagickImage img = new MagickImage(jpg);
    img.Format = MagickFormat.Pdf;
    collection.Add(img);
  }
  collection.Write(@"c:\PDF\out.pdf");
}
May 7, 2014 at 6:12 AM
Thank you for reply!


dlemstra wrote:
If you want to create a PDF with 300 images you will first have to read all of them in memory. You could use the Q8 instead of the Q16 version of Magick.NET to reduce the memory that is being used.

You cannot write a multipage PDF file by appending to the file. You will have to write the whole file at once:
i have little questions about it:
you said i have to write the whole file at once. is it the only way to do it? i mean is the inability to write a multipage PDF file by appending to the file due to pdf specification or to implementation of magick.net?

thx
Coordinator
May 7, 2014 at 10:47 AM
This is the only way to do it with Magick.NET (ImageMagick). The pdf specifications don't allow appending to the file. Maybe you should find another tool to combine multiple images into one pdf that does not load all the bitmap data.
May 7, 2014 at 12:09 PM
Thank you for reply!
I can't find such tool. so I will find another way to do required work.
thanks!