Convert Files with the Document Converter - C# .NET 6

This tutorial shows how to convert between raster and document file formats in a C# .NET 6 application using the LEADTOOLS Document Converter SDK.

The raster to raster conversion is between JPEG file format and TIFF and for the raster to document conversion is between TIF file format and searchable PDF.

Overview  
Summary This tutorial covers how to use the DocumentConverter class in a C# .NET 6 application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 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

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 this 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 DLL files are required for your application, refer to Files to be Included With Your Application.

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:

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) 
{ 
   InitLEAD(); 
 
   string directory = @"C:\LEADTOOLS23\Resources\Images"; 
   using IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
   using DocumentConverter docConverter = new DocumentConverter(); 
   ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS23\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:

Convert Between Raster Images

C#
// Convert JPEG 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 ext = 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 Converted {file} to {outFile}\n"); 
   } 
} 

Convert Between Raster Images and Searchable PDF Documents

C#
// Convert TIFF 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 ext = DocumentWriter.GetFormatFileExtension(docFormat); 
      string outFile = Path.Combine(outputDir, $"{fileName}.{ext}"); 
 
      DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(file, outFile, docFormat); 
      jobData.JobName = "Convert to Document 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 Converted {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>\LEADTOOLS23\Resources\Images\Converted

Wrap-up

This tutorial showed how to convert files to raster image formats and document formats using the DocumentConverter class.

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.