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