PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Concatenating large number of pdf : OutOfMemory Exception...
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1307
Page 1 of 1

Author:  advapi [ Fri Aug 27, 2010 2:59 pm ]
Post subject:  Concatenating large number of pdf : OutOfMemory Exception...

Hello,
I need to concatenate a large number of pdf (almost 18.000) each one about 200kb, I've used the code present on the wiki

Code:
    FileStream sw = File.Open("c:\\temp\\output1.pdf", FileMode.Create, FileAccess.Write);
            // Open the output document
            PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument(sw);

            int iTotalReportPage = 0;
            int totalItems = pdf.Count;

            // Iterate files
            for(int i =0;i<pdf.Count;i++)
            {
                DateTime dtStart2 = DateTime.Now;
                // Open the document to import pages from it.
                PdfDocument inputDocument = PdfReader.Open(pdf[i], PdfDocumentOpenMode.Import);

                int pages = inputDocument.PageCount;
               // int pagesDopoScrittura = iTotalReportPage + pages;

                // Iterate pages
                int count = inputDocument.PageCount;
                for (int idx = 0; idx < count; idx++)
                {
                    // Get the page from the external document...
                    PdfPage page = inputDocument.Pages[idx];
                    // ...and add it to the output document.
                    outputDocument.AddPage(page);
                }

                inputDocument.Clone();
                iTotalReportPage += pages;
                // tw.WriteLine(riepilogo);
                DateTime dtEnd2 = DateTime.Now;
                Console.WriteLine(string.Format("Processato {0}/{1} - Tempo {2}", i, totalItems, (dtEnd2 - dtStart2).TotalMilliseconds));
            }

            // Save the document...
            string filename = "c:\\temp\\ConcatenatedDocument1.pdf";
            outputDocument.Save(filename);
            // ...and start a viewer.
            //  Process.Start(filename);


But I receive an out of memory exception... I can understand the problem , the outputDocument is kept in memory till I do the Save (I've tried using a Filestream but even the orinal version is failing)

It's possible to write each file directly instead of keeping in memory?
Thanks
Paolo

Author:  () => true [ Thu Sep 02, 2010 7:30 pm ]
Post subject:  Re: Concatenating large number of pdf : OutOfMemory Exception...

advapi wrote:
It's possible to write each file directly instead of keeping in memory?

PDFsharp was designed to keep everything in memory.
Sounds like a major change .

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/