Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: append images to output pdf file

$
0
0
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!

Viewing all articles
Browse latest Browse all 3693

Trending Articles