Merge Documents with LEADDocument - .NET Core

This tutorial shows how to create a C# .NET Core application that creates a new LEADDocument that loads documents from a directory and adds them to the created LEADDocument. This tutorial also shows how to output the created merged LEADDocument to stream.

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

Required Knowledge

Before working on the Merge Document with LEADDocument - .NET Core tutorial, you need to be familiar with the basic steps of creating a project by reviewing the Add References and Set a License Tutorial tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License Tutorial tutorial. If you do not have that project, follow the steps in that tutorial to create it.

This tutorial requires the following NuGet package:

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

How to properly add LEADTOOLS NuGet references is covered in theAdd 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 and add the below using statements to the block at the top:

C#
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Document; 
using Leadtools.Document.Converter; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 

Add a new method called MemoryStream MergePdfFiles(string dir, IOcrEngine ocrEngine) to return the stream containing 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#
static void Main(string[] args) 
{ 
   if (!SetLicense()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
   string folder = @"C:\LEADTOOLS21\Resources\Images"; 
   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(); 
} 
 
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 its 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 covered how to use the LEADDocument and DocumentConverter classes.

See Also

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


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