PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Sat Jul 05, 2025 12:48 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sat Mar 12, 2016 10:30 am 
Offline

Joined: Sat Mar 12, 2016 10:19 am
Posts: 4
Hi,

wrote code which works, but the bigger the files the more problems I get, how can I optimise my code without running in outofmomory

Code:
` private static void ConvertImagetoPDF(String[] ImageFilenames, string[] directories, string insertName = "", string prefix = "Result", string usersChoise = "1", bool tempLabel = false, double tempHeight = 6000, double tempWidth = 8000)
        {
            try
            {
                PdfDocument document = new PdfDocument();
                string temp = "";

                try
                {
                    Console.WriteLine(globalUsersChoise(ref usersChoise, ref prefix, ref tempHeight, ref tempWidth, ref tempLabel));
                    //todo: calculate how many pages it can add in one rush
                    Image[] img = new Image[ImageFilenames.Length];

                    for (int i = 0; i < ImageFilenames.Length; i++)
                    //for (int i = 0; i < 4; i++)
                    {
                        img[i] = Image.FromFile(ImageFilenames[i]);


                        // Create an empty page or load existing
                        PdfPage page = document.AddPage();


                        // Get an XGraphics object for drawing
                        XGraphics gfx = XGraphics.FromPdfPage(page);
                        XImage image = XImage.FromFile(ImageFilenames[i]);

                        page.Width = tempWidth;
                        page.Height = tempHeight;

                        float width = image.PixelWidth * 0.3f;
                        float height = image.PixelHeight * 0.3f;

                        #region create Label

                        // Create a font
                        XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);

                        // Draw the text
                        if (tempLabel)
                        {
                            gfx.DrawString(Path.GetFileNameWithoutExtension(ImageFilenames[i]), font, XBrushes.Black,
                             new XRect(120, -30, (page.Width - width), (page.Height - height)),
                             XStringFormats.Center);
                        }

                        #endregion


                        gfx.DrawImage(image, (page.Width / 2) - (width / 2), (page.Height / 2) - (height / 2), width, height);//put it in the middle

                        Console.WriteLine((i + 1) + ": " + Path.GetFileName(ImageFilenames[i]));
                        GC.Collect();//hoped this would do the job
                        //document.Save(@"..\..\sample" + Path.GetFileNameWithoutExtension(ImageFilenames[i]) + ".pdf");                             
                    }



                    //var temp = @"!" + prefix + DateTime.Now.Ticks.ToString() + ".pdf";
                }
                finally   //will execute this as it is the first exception statement
                { //save the pdf pagedocument
                    temp = @"!" + prefix + "_" + (insertName.Equals("") ? DateTime.Now.Ticks.ToString() : insertName.ToString().Replace(":", "")) + ".pdf";

                    document.Save(temp);
                    document.Close();
                    Process.Start(temp);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();

            }

        }


I hoped cleaning the GarbageCollector would help....

Thank you for supporting :)


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 12, 2016 3:52 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1051
Location: CCAA
Hi!

Use "using" for disposable objects like XImage.

Run the program in 64 bit mode to break the 2 GiB barrier.

Save and close the file from time to time, then open it to add more images.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 13, 2016 10:17 am 
Offline

Joined: Sat Mar 12, 2016 10:19 am
Posts: 4
Thank you,

but how exactly can I calculate that moment for "Save and close the file from time to time, then open it to add more images." ?


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 13, 2016 6:02 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1051
Location: CCAA
JoeTHeRock wrote:
but how exactly can I calculate that moment for [...]
Trial and error.

With .NET 4.5.1 you can try a complete garbage collection:
https://msdn.microsoft.com/en-us/librar ... e(v=vs.110).aspx


Test how many MB of images you can process without error - and then maybe use 25% as trigger for production code.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group