We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

PdfDocument.Save() slows down

Hi,

I want to save a large number of pages into a PDF document with as little RAM consumption as possible. My approach is to call a document.Save after each page to save the changes into a file stream. I noticed that the save method takes longer each time I write a new page into the stream. Do I muss something?

Here the code sippet:
using (var outStream = File.Create("./sample.pdf"))
{
// Create a new PDF document.
PdfDocument document = new PdfDocument();
var sw = Stopwatch.StartNew();
for (var p = 0; p < 100; p++)
{
// Add a page to the document.
PdfPage page = document.Pages.Add();
// Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
// Load the image from the disk.
using (var ifs = File.Open("syncfusion-logo.png", FileMode.Open))
{
PdfBitmap image = new PdfBitmap(ifs);
// Draw the image
graphics.DrawImage(image, 0, 0, 200, 200);
}
var prev = sw.ElapsedMilliseconds;
// Save the document.
document.Save(outStream);
var curr = sw.ElapsedMilliseconds;
Console.WriteLine($"Page {p} / {numberOfPages} : {curr - prev}ms");
}
// Save the document.
document.Save(outStream);
// Close the document.
document.Close(true);
}

And here the console output:
Page 1 / 100 : 4ms
Page 2 / 100 : 4ms
Page 3 / 100 : 3ms
Page 4 / 100 : 4ms
Page 5 / 100 : 4ms
Page 6 / 100 : 4ms
Page 7 / 100 : 5ms
Page 8 / 100 : 5ms
Page 9 / 100 : 6ms
Page 10 / 100 : 7ms
Page 11 / 100 : 7ms
Page 12 / 100 : 8ms
Page 13 / 100 : 8ms
Page 14 / 100 : 10ms
Page 15 / 100 : 9ms
Page 16 / 100 : 11ms
Page 17 / 100 : 12ms
Page 18 / 100 : 18ms
Page 19 / 100 : 10ms
Page 20 / 100 : 12ms
Page 21 / 100 : 11ms
Page 22 / 100 : 12ms
Page 23 / 100 : 14ms
Page 24 / 100 : 13ms
Page 25 / 100 : 16ms
Page 26 / 100 : 18ms
Page 27 / 100 : 21ms
Page 28 / 100 : 15ms
Page 29 / 100 : 16ms
Page 30 / 100 : 18ms
Page 31 / 100 : 16ms
Page 32 / 100 : 19ms
Page 33 / 100 : 18ms
Page 34 / 100 : 18ms
Page 35 / 100 : 20ms
Page 36 / 100 : 20ms
Page 37 / 100 : 20ms
Page 38 / 100 : 22ms
Page 39 / 100 : 20ms
Page 40 / 100 : 21ms
Page 41 / 100 : 24ms
Page 42 / 100 : 30ms
Page 43 / 100 : 27ms
Page 44 / 100 : 26ms
Page 45 / 100 : 29ms
Page 46 / 100 : 26ms
Page 47 / 100 : 26ms
Page 48 / 100 : 25ms
Page 49 / 100 : 27ms
Page 50 / 100 : 36ms
Page 51 / 100 : 28ms
Page 52 / 100 : 29ms
Page 53 / 100 : 28ms
Page 54 / 100 : 30ms
Page 55 / 100 : 27ms
Page 56 / 100 : 29ms
Page 57 / 100 : 36ms
Page 58 / 100 : 30ms
Page 59 / 100 : 30ms
Page 60 / 100 : 33ms
Page 61 / 100 : 31ms
Page 62 / 100 : 31ms
Page 63 / 100 : 40ms
Page 64 / 100 : 33ms
Page 65 / 100 : 35ms
Page 66 / 100 : 34ms
Page 67 / 100 : 35ms
Page 68 / 100 : 36ms
Page 69 / 100 : 39ms
Page 70 / 100 : 39ms
Page 71 / 100 : 36ms
Page 72 / 100 : 37ms
Page 73 / 100 : 37ms
Page 74 / 100 : 42ms
Page 75 / 100 : 38ms
Page 76 / 100 : 40ms
Page 77 / 100 : 38ms
Page 78 / 100 : 40ms
Page 79 / 100 : 43ms
Page 80 / 100 : 41ms
Page 81 / 100 : 43ms
Page 82 / 100 : 41ms
Page 83 / 100 : 40ms
Page 84 / 100 : 47ms
Page 85 / 100 : 43ms
Page 86 / 100 : 43ms
Page 87 / 100 : 46ms
Page 88 / 100 : 44ms
Page 89 / 100 : 51ms
Page 90 / 100 : 46ms
Page 91 / 100 : 46ms
Page 92 / 100 : 48ms
Page 93 / 100 : 49ms
Page 94 / 100 : 50ms
Page 95 / 100 : 48ms
Page 96 / 100 : 48ms
Page 97 / 100 : 54ms
Page 98 / 100 : 53ms
Page 99 / 100 : 51ms

Thank you in advance.

3 Replies

DB Dilli Babu Nandha Gopal Syncfusion Team October 17, 2018 12:41 PM UTC

Hi Christian, 
 
As per our current architecture we are using common resources for creating and organizing PDF pages, we could not save the individual page to the file stream at the time of page creation. The pages are saves while calling the Save method of the PDF document. So, the provided code snippet produced more memory consumption and large output file size. for example,  
  1.  First iteration - the first page and its objects are saved to the document
  2. Second iteration - the first- and second-page objects are saved together and so on
so, it lacks performance and more memory for the output file. 
 
We suggest you use the file stream instead of using memory stream to save the PDF document to avoid more runtime memory usage. please refer the below code snippet for further details.  

using (var outStream = File.Create("sample.pdf"))  
{ 
    // Create a new PDF document. 
    PdfDocument document = new PdfDocument(); 
 
    // Load the image from the disk. 
    using (var ifs = File.Open("../../syncfusion_logo.gif", FileMode.Open)) 
    { 
        PdfBitmap image = new PdfBitmap(ifs); 
 
        var sw = Stopwatch.StartNew(); 
        for (var p = 0; p < 100; p++) 
        { 
            // Add a page to the document. 
            PdfPage page = document.Pages.Add(); 
 
            // Create PDF graphics for the page. 
            PdfGraphics graphics = page.Graphics; 
            // Draw the image 
            graphics.DrawImage(image, 0, 0, 200, 200); 
        } 
    }            
 
    // Save the document. 
    document.Save(outStream);                 
}         

 

Please let us know if you have any questions. 

Regards, 
Dilli babu. 



CD Christian Drüke October 18, 2018 11:55 AM UTC

Hi Dilli Badu,

thank your for your reply.
I teste the code you've send me. I added some time measurement to it and and increase the number of pages to 20000. Even if it is an unusually high number, it is certainly a realistic scenario for us. The processing time per page is still increasing. Do I miss something.

Best regards
Christian

Here my code and the results
using (var outStream = File.Create("sample.pdf"))
            {
                var sw = Stopwatch.StartNew();
                long prev = 0;
                long curr = 0;

                // Create a new PDF document.
                PdfDocument document = new PdfDocument();

                // Load the image from the disk.
                using (var ifs = File.Open("./syncfusion-logo.png", FileMode.Open))
                {
                    PdfBitmap image = new PdfBitmap(ifs);
                    for (var p = 0; p < 20000; p++)
                    {
                        // Add a page to the document.
                        PdfPage page = document.Pages.Add();

                        // Create PDF graphics for the page.
                        PdfGraphics graphics = page.Graphics;

                        // Draw the image
                        graphics.DrawImage(image, 0, 0, 200, 200);

                        if (p > 0 && p % 1000 == 0)
                        {
                            curr = sw.ElapsedMilliseconds;
                            Console.WriteLine($"Page {p} / {20000}: {curr - prev}ms");
                            prev = sw.ElapsedMilliseconds; ;
                        }
                    }

                    curr = sw.ElapsedMilliseconds;
                    Console.WriteLine($"Page {20000} / {20000}: {curr - prev}ms");
                }

                // Save the document.
                document.Save(outStream);

                // Close the document.
                document.Close(true);
            }
Page 1000 / 20000: 101ms
Page 2000 / 20000: 125ms
Page 3000 / 20000: 188ms
Page 4000 / 20000: 189ms
Page 5000 / 20000: 214ms
Page 6000 / 20000: 299ms
Page 7000 / 20000: 354ms
Page 8000 / 20000: 373ms
Page 9000 / 20000: 419ms
Page 10000 / 20000: 556ms
Page 11000 / 20000: 620ms
Page 12000 / 20000: 665ms
Page 13000 / 20000: 864ms
Page 14000 / 20000: 886ms
Page 15000 / 20000: 1137ms
Page 16000 / 20000: 1251ms
Page 17000 / 20000: 1492ms
Page 18000 / 20000: 1816ms
Page 19000 / 20000: 1332ms
Page 20000 / 20000: 1250ms




DB Dilli Babu Nandha Gopal Syncfusion Team October 22, 2018 09:16 AM UTC

Hi Christian, 

We have confirmed that the reported issue with “Performance issue occurs while adding pages to new PDF document” is a defect. The issue fix will be included in our upcoming main release i.e., 2018 Volume 4 which is estimated to be available by December 2018. 

Regards, 
Dilli babu. 


Loader.
Live Chat Icon For mobile
Up arrow icon