Convert Files with Document Converter - Console C#

This tutorial shows how to create a C# Windows console 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# 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

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 Convert Files with Document Converter - 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.

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 references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Convert to Document Code

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

In the Solution Explorer, open Program.cs. Add two new methods called ConvertToRaster(string directory, DocumentConverter docConverter, RasterImageFormat imageFormat) and ConvertToDocument(string directory, DocumentConverter docConverter, DocumentFormat docFormat) and call them inside the Main method below SetLicense();. Add the below code to initialize the IOcrEngine and the DocumentConverter, and convert files to raster image formats and document formats. A valid OCR engine is required for converting raster images to document formats.

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; 
 
namespace Convert_Files_with_Document_Converter 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            string directory = @"C:\LEADTOOLS21\Resources\Images"; 
            SetLicense(); 
            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); 
            } 
        } 
    } 
} 
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 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 Convereted {file} to {outFile}\n"); 
    } 
} 
C#
// 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 ext = 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.2020.10.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.