iText7 for .NET is 4-9 times slower than iTextSharp 5.5.9 while merging files
<p>I've tried using iText7 for .NET for merging documents into single one, after getting some errors I've created second application but using iTextSharp 5.5.9.</p>

<p>I was amazed that same functionality works 4 times faster than in newer version.</p>

<p>Below is my code for version 5:</p>

<pre><code>private bool Merge5(IEnumerable&lt;string&gt; fileNames, string targetPdf)
{
    bool merged = true;
    using (FileStream stream = new FileStream(targetPdf, FileMode.Create))
    {
        Document document = new Document();
        PdfCopy pdf = new PdfCopy(document, stream);
        PdfReader reader = null;
        try
        {
            document.Open();
            foreach (string file in fileNames)
            {
                reader = new PdfReader(file);
                //not needed I guess
                reader.ConsolidateNamedDestinations();
                pdf.AddDocument(reader);
                reader.Close();
            }
        }
        catch (Exception)
        {
            merged = false;
            if (reader != null)
            {
                reader.Close();
            }
        }
        finally
        {
            if (pdf != null)
            {
                pdf.Close();
            }
            if (document != null)
            {
                document.Close();
            }
        }
    }
    return merged;
}
</code></pre>

<p>and code for version 7:</p>

<pre><code>private void Merge7(List&lt;string&gt; src, string dest)
{
    PdfDocument pdfDocument1;

    try
    {
        pdfDocument1 = new PdfDocument(new PdfReader(src[0]), new PdfWriter(dest));
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
        throw;
    }

    var path = string.Empty;

    for (int i = 1, max = src.Count; i &lt; max; i++)
    {
        path = src[i];
        try
        {
            PdfDocument pdfDocument2 = new PdfDocument(new PdfReader(path));
            var pagesCount = pdfDocument2.GetNumberOfPages();
            pdfDocument2.CopyPagesTo(1, pagesCount, pdfDocument1);
            pdfDocument2.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
    try
    {
        pdfDocument1.Close();
    }
    catch (Exception e)
    {
        Debug.WriteLine("Dest: " + dest);
        Debug.WriteLine("Src: " + path);
        Console.WriteLine(e);
    }
}
</code></pre>

<p>My call looks like this:</p>

<pre><code>string dest = @"E:\TEST\final.pdf";
var files = Directory.GetFiles(@"E:\TEST\PDFS", "*.pdf").OrderBy(x =&gt; x).Take(1000).ToList();


Stopwatch sw = new Stopwatch();
sw.Start();

Merge5(files, dest);

sw.Stop();
Debug.WriteLine(sw.Elapsed);
</code></pre>

<p>First test:</p>

<p>For 1000 PDF files (same file, but copied 1000 times, size 370 KB) I get this results:</p>

<pre><code>                 |           1 pass |           2 pass |           3 pass |
---------------------------------------------------------------------------
iText7 for .NET  | 00:00:13.4088770 | 00:00:13.5490370 | 00:00:14.2491171
iTextSharp 5.5.9 | 00:00:03.5330538 | 00:00:03.2058272 | 00:00:03.2854776
</code></pre>

<p>Second test:</p>

<p>For 1000 PDF files (same file, but copied 1000 times, size 606 KB) I get this results:</p>

<pre><code>                 |           1 pass |           2 pass |           3 pass |
---------------------------------------------------------------------------
iText7 for .NET  | 00:00:25.5538607 | 00:00:24.6525861 | 00:00:26.7326629
iTextSharp 5.5.9 | 00:00:06.0918370 | 00:00:05.5687955 | 00:00:06.0283861
</code></pre>

<p>What might be the reason why I get so large difference in performance?<br>
Can I optimize my merging function so that it will be faster (fast as in version 5)?<br>
I'd like to use version 7, but because of performance I'll probably use older version.</p>