Convert Images to Searchable PDF with OCR - WinForms C#

This tutorial shows how to create a C# Windows WinForms application that sets up the LEAD OCR Engine to process OCR.

Overview  
Summary This tutorial covers how to set up the LEAD OCR Engine in a C# Windows WinForms application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (10 KB)
Platform C# Windows WinForms Application
IDE Visual Studio 2017, 2019
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 and Display Images in an Image Viewer tutorials, before working on the Convert Images to Searchable PDF with OCR - WinForms C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Display Images in an Image Viewer tutorial. If the project is not available, follow the steps in that tutorial to create it. The code for saving the loaded image as JPEG from that tutorial is not needed in this tutorial, so it can be omitted.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet packages and their dependencies:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\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 local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize the IOcrEngine Code

In the Solution Explorer, open Form1.cs. In the designer right-click and select View Code, or press F7, to bring up the code behind the form. Ensure that the following using statements and global variables are added.

C#
// Add these to "using" block at the top 
using Leadtools.Ocr; 
using Leadtools.Document.Writer; 
C#
// Add global variables 
private IOcrEngine _ocrEngine; 
private IOcrPage _ocrPage; 

Add code inside the Form1_Load event handler to initialize the IOcrEngine, so that it becomes as follows:

C#
private void Form1_Load(object sender, EventArgs e) 
{ 
   // Initialize Image Viewer object 
   _viewer = new ImageViewer(); 
   _viewer.Dock = DockStyle.Fill; 
   _viewer.BackColor = Color.DarkGray; 
   _viewer.Zoom(ControlSizeMode.FitWidth, 1.0, new LeadPoint()); 
   Controls.Add(_viewer); 
   _viewer.BringToFront(); 
 
   // Initialize the OCR engine  
   _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
   // Start up the engine  
   _ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
} 

Add code inside the openToolStripMenuItem_Click event handler to initialize the IOcrPage after an image is loaded in the viewer:

C#
private void openToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      using (RasterCodecs codecs = new RasterCodecs()) 
      { 
         OpenFileDialog dlg = new OpenFileDialog(); 
         dlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
         if (dlg.ShowDialog(this) == DialogResult.OK) 
         { 
            _viewer.Image = codecs.Load(dlg.FileName); 
         } 
      } 
 
      // Create IOcrPage from loaded image 
      _ocrPage = _ocrEngine.CreatePage(_viewer.Image, OcrImageSharingMode.AutoDispose); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Add the Save as Searchable PDF Code

In the Solution Explorer, once again open Form1.cs. In the designer, add a new MenuItem to the File menu in the MenuStrip. Set this new MenuItem's text to Save as Searchable &PDF. You can leave the new item's name as saveAsSearchablePDFToolStripMenuItem_Click.

Adding Save As Searchable PDF menu item

Double-click the Save as Searchable PDF menu item to edit its event handler. Add the following code in it:

C#
private void saveAsSearchablePDFToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      // Create a document  
      using (IOcrDocument ocrDocument = _ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile)) 
      { 
         // Create IOcrPage from loaded image 
         _ocrPage = _ocrEngine.CreatePage(_viewer.Image, OcrImageSharingMode.AutoDispose); 
         // Recognize Text 
         _ocrPage.Recognize(null); 
         // Add the page  
         ocrDocument.Pages.Add(_ocrPage); 
         // Save as PDF 
         SaveFileDialog saveDlg = new SaveFileDialog(); 
         saveDlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
         saveDlg.Filter = "Adobe Portable Document Format|*.pdf"; 
         if (saveDlg.ShowDialog(this) != DialogResult.OK) 
            return; 
         ocrDocument.Save(saveDlg.FileName, DocumentFormat.Pdf, null); 
         MessageBox.Show($"OCR output saved to {saveDlg.FileName}"); 
      } 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

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 is able to load and display any image that is supported by the above codecs filters. When Save as Searchable PDF is pressed, the application recognizes the text in the loaded image (OCR1.TIF, for example) and saves it to the specified location (as a searchable PDF).

Wrap-up

This tutorial showed how to create a simple Windows Forms OCR application that initializes the LEAD OCR Engine, displays a specified input file, and outputs the image as a searchable PDF.

See Also

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

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