Convert Files with the Document Converter - .NET Core

This tutorial shows how to create a C# .NET Core application that converts files using the Document Converter class.

Overview  
Summary This tutorial covers how to use LEADTOOLS Document Converter SDK technology 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 Convert Files with the Document Converter - .NET Core tutorial, get 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 the project is not available, 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.

Add the File Conversion Code

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

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

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

A valid OCR engine is required for converting raster images to document formats. Add the below code to initialize the IOcrEngine and the DocumentConverter.

C#
static void Main(string[] args) 
{ 
   if (!SetLicense()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
   string directory = @"C:\LEADTOOLS21\Resources\Images"; 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   using (DocumentConverter docConverter = new DocumentConverter()) 
   { 
      ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
      docConverter.SetOcrEngineInstance(ocrEngine, false); 
      // Change the RasterImageFormat enumeration to whichever is needed 
      ConvertToRaster(directory, docConverter, RasterImageFormat.Tif); 
      // Change the DocumentFormat enumeration to whichever is needed 
      ConvertToDocument(directory, docConverter, DocumentFormat.Pdf); 
   } 
} 

Next add two new methods named ConvertToRaster(string directory, DocumentConverter docConverter, RasterImageFormat imageFormat) and ConvertToDocument(string directory, DocumentConverter docConverter, DocumentFormat docFormat). These methods will be called in the Main() method, as shown above. Below is the code for the 2 new methods:

C#
// Convert files to raster images 
static void ConvertToRaster(string directory, DocumentConverter docConverter, RasterImageFormat imageFormat) 
{ 
   // Pull all JPEG files from directory to convert to TIFF 
   string[] files = Directory.GetFiles(directory, "*.jpg"); 
   string outputDir = Path.Combine(directory, "Converted"); 
 
   if (!Directory.Exists(outputDir)) 
      Directory.CreateDirectory(outputDir); 
 
   foreach (string file in files) 
   { 
      Console.WriteLine($"Converting {file}..."); 
      string fileName = Path.GetFileNameWithoutExtension(file); 
      string text = RasterCodecs.GetExtension(imageFormat); 
      string outFile = Path.Combine(outputDir, $"{fileName}.{ext}"); 
 
      DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(file, outFile, imageFormat); 
      jobData.JobName = "Convert to Image Job"; 
 
      DocumentConverterJob job = docConverter.Jobs.CreateJob(jobData); 
      docConverter.Jobs.RunJob(job); 
 
      if (job.Errors.Count > 0) 
         foreach (var error in job.Errors) 
            Console.WriteLine($"Error during conversion: {error.Error.Message}\n"); 
      else 
         Console.WriteLine($"Successfully Convereted {file} to {outFile}\n"); 
   } 
} 
 
// Convert files to searchable PDF 
static void ConvertToDocument(string directory, DocumentConverter docConverter, DocumentFormat docFormat) 
{ 
   // Pull all TIFF files from directory to convert to searchable PDF 
   string[] files = Directory.GetFiles(directory, "*.tif"); 
   string outputDir = Path.Combine(directory, "Converted"); 
 
   if (!Directory.Exists(outputDir)) 
      Directory.CreateDirectory(outputDir); 
 
   foreach (string file in files) 
   { 
      Console.WriteLine($"Converting {file}..."); 
      string fileName = Path.GetFileNameWithoutExtension(file); 
      string text = DocumentWriter.GetFormatFileExtension(docFormat); 
      string outFile = Path.Combine(outputDir, $"{fileName}.{ext}"); 
 
      DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(file, outFile, docFormat); 
      jobData.JobName = "Convert to Image Job"; 
 
      DocumentConverterJob job = docConverter.Jobs.CreateJob(jobData); 
      docConverter.Jobs.RunJob(job); 
 
      if (job.Errors.Count > 0) 
         foreach (var error in job.Errors) 
            Console.WriteLine($"Error during conversion: {error.Error.Message}\n"); 
      else 
         Console.WriteLine($"Successfully Convereted {file} to {outFile}\n"); 
   } 
} 

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 converts all the JPEG files in the Images directory to TIFF, and converts all the TIFF files in the Images directory to searchable PDF. All converted files should output to this folder: <INSTALL_DIR>\LEADTOOLS21\Resources\Images\Converted

Wrap-up

This tutorial showed how to convert files to raster image formats and document formats. Also, it covered how to use the DocumentConverter class.

See Also

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