2

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.

I was amazed that same functionality works 4 times faster than in newer version.

Below is my code for version 5:

private bool Merge5(IEnumerable<string> 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;
}

and code for version 7:

private void Merge7(List<string> 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 < 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);
    }
}

My call looks like this:

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


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

Merge5(files, dest);

sw.Stop();
Debug.WriteLine(sw.Elapsed);

First test:

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

                 |           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

Second test:

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

                 |           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

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

4

0 回答 0