Merge Documents with LEADDocument - C# .NET 6

This tutorial shows how to create a new LEADDocument that loads and merges documents from a directory or to a stream in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to merge documents using LEADDocument in a C# .NET 6 application
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET 6+ (Console)

  • Python: Python

Required Knowledge

Before working on the Merge Document with LEADDocument - .NET 6 tutorial, you need to be familiar with the basic steps of creating a project by reviewing the Add References and Set a License 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. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

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:

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 (!InitLEAD()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
   string folder = @"C:\LEADTOOLS23\Resources\Images"; 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"); 
      MemoryStream ms = MergePdfFiles(folder, ocrEngine); 
      ms.Position = 0; 
      File.WriteAllBytes(@"C:\LEADTOOLS23\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 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

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