Merge Documents with LEADDocument - Console C#

This tutorial shows how to create a C# Windows console application that creates a new LEADDocument, loads documents from a directory, then adds them to the created LEADDocument, and lastly outputs the created merged LEADDocument to stream.

Overview  
Summary This tutorial covers how to merge documents using LEADDocument in a C# Windows Console application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Merge Documents with LEADDocument - Console C# tutorial.

Create the Project and Add the LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Create LEADDocument and Merge Documents Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add a new method called MemoryStream MergePdfFiles(string dir, IOcrEngine ocrEngine) to return the stream for the merged PDF document. Add the below code to create a new LEADDocument, merge PDF documents, and gather the stream from the created PDF document.

C#
// Add to using block 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Document; 
using Leadtools.Document.Converter; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 
C#
static void Main(string[] args) 
{ 
    string folder = @"C:\LEADTOOLS21\Resources\Images"; 
    SetLicense(); 
    using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
    { 
        ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
        MemoryStream ms = MergePdfFiles(folder, ocrEngine); 
        ms.Position = 0; 
        File.WriteAllBytes(@"C:\LEADTOOLS21\Resources\Images\merged.pdf", ms.GetBuffer()); 
    } 
 
    Console.WriteLine("Press any key to exit..."); 
    Console.ReadKey(); 
} 
C#
static MemoryStream MergePdfFiles(string dir, IOcrEngine ocrEngine) 
{ 
    DocumentWriter documentWriter = new DocumentWriter(); 
    // Get the current PDF options  
    PdfDocumentOptions pdfOptions = documentWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions; 
    // Set our options  
    documentWriter.SetOptions(DocumentFormat.Pdf, pdfOptions); 
    pdfOptions.ImageOverText = true; 
    var outputStream = new MemoryStream(); 
    var createOptions = new CreateDocumentOptions(); 
    LEADDocument virtualDocument = DocumentFactory.Create(createOptions); 
    virtualDocument.AutoDisposeDocuments = true; 
    virtualDocument.Name = "Virtual"; 
 
    string[] files = Directory.GetFiles(dir, "*.pdf"); 
    foreach (var file in files) 
    { 
        LEADDocument childDocument = DocumentFactory.LoadFromFile(file, new LoadDocumentOptions()); 
        virtualDocument.Pages.AddRange(childDocument.Pages); 
    } 
 
    // Convert virtualDocument using DocumentConverter to finalize the document and gather the stream 
    DocumentConverter docConverter = new DocumentConverter(); 
    docConverter.SetOcrEngineInstance(ocrEngine, false); 
    docConverter.SetDocumentWriterInstance(documentWriter); 
    var jobData = new DocumentConverterJobData 
    { 
        Document = virtualDocument, 
        OutputDocumentStream = outputStream, 
        DocumentFormat = Leadtools.Document.Writer.DocumentFormat.Pdf 
    }; 
    var job = docConverter.Jobs.CreateJob(jobData); 
    docConverter.Jobs.RunJob(job); 
    if (job.Status == DocumentConverterJobStatus.Success) 
    { 
        Console.WriteLine($"Success!"); 
    } 
    else 
    { 
        Console.WriteLine("{0} Errors", job.Status); 
        foreach (var error in job.Errors) 
        { 
            Console.WriteLine("  {0} at {1}: {2}", error.Operation, error.InputDocumentPageNumber, error.Error.Message); 
        } 
    } 
 
    return outputStream; 
} 

Note

Adding pages from child documents to a Virtual Document is not finalized. This means that the source pages from the child documents still only exist in the location they were originally loaded from. The Virtual Document only contains the information for where each page exists as well as other metadata about the page and file. - The Virtual Document can be displayed in a Document Viewer. - Or the Virtual Document can be finalized and a new Document can be created by using the Document Converter as illustrated in this tutorial. This creates a legitimate document that contains copies of the source pages in it's own document structure.

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and creates a new virtual LEADDocument. The application then takes each PDF file from a given directory and adds each PDF file to the virtual LEADDocument. Lastly, it "finalizes" the virtual document by sending it to the Document Converter.

Wrap-up

This tutorial showed how to create a new LEADDocument, add PDF documents to the LEADDocument, and gather the stream to the new LEADDocument. Also it covered how to use the LEADDocument and DocumentConverter classes.

See Also

Help Version 21.0.2021.6.17
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.