I'm using PDFsharp to merge many PDFs (stored on disk) into one PDF. Sometimes the end product PDF can be as large as 700MB. I'm using the sample code provided that basically creates an output PdfDocument, adds pages to it, and then calls outputDocument.Save(destinationPath), so the amount of memory used is about the same as the size of documents produced. Here's a link to the sample:
http://www.pdfsharp.net/wiki/concatenat ... ample.ashxI tried to use a FileStream in the constructor of PdfDocument when creating the output, that did not seem to work. Somebody suggested that I write a certain number of files, close the PDF, re-open using PdfReader.Open() and continue. Not sure how that would work seeing as I think PdfReader.Open() will load the whole document in memory as far as I know, but I tried it and sure enough it did not look like memory consumption decreased.
Below is the code for a simple console app that tries to merge 2000 files, it closes the doc every 500 pages and re-opens. I'm using PDFsharp-MigraDoc-gdi 1.50.5147, targeting .NET Framework 4.5.
If this cannot be done with PdfSharp, would MigraDocs be any help?
Code:
using System;
using System.Collections.Generic;
using System.IO;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace PdfSharpMergeTest
{
class Program
{
public static void Main(string[] args)
{
var files = new List<string>();
var basePath = AppDomain.CurrentDomain.BaseDirectory;
for (var i = 0; i < 2000; i++)
{
files.Add($"{basePath}\\sample.pdf");
}
DoMerge(files, $"{basePath}\\output.pdf");
}
private static void DoMerge(List<string> paths, string destinationFile)
{
var directory = Path.GetDirectoryName(destinationFile);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var outputDocument = new PdfDocument();
var count = 0;
// Iterate files
foreach (string path in paths)
{
// Open the document to import pages from it.
try
{
var inputDocument = PdfReader.Open(path, PdfDocumentOpenMode.Import);
// Iterate pages
for (int idx = 0; idx < inputDocument.PageCount; idx++)
{
// Get the page from the external document...
PdfPage page = inputDocument.Pages[idx];
// ...and add it to the output document.
outputDocument.AddPage(page);
}
inputDocument.Dispose();
count++;
if (count % 500 == 0 || count == paths.Count)
{
outputDocument.Save(destinationFile);
outputDocument.Dispose();
if (count < paths.Count)
{
outputDocument = PdfReader.Open(destinationFile, PdfDocumentOpenMode.Import);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}
}