This tutorial shows how to create a Windows WinForms C# .NET 6 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 Windows WinForms C# .NET 6 application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (9 KB) |
Platform | C# Windows WinForms Application |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
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.
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:
Leadtools.Ocr
Leadtools.Viewer.Controls.WinForms
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Controls.WinForms.dll
Leadtools.Core.dll
Leadtools.Document.Writer.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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.
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.
using Leadtools;
using Leadtools.Controls;
using Leadtools.Codecs;
using Leadtools.Ocr;
using Leadtools.Document.Writer
private ImageViewer _viewer;
private IOcrEngine _ocrEngine;
private IOcrPage _ocrPage;
Add code inside the Form1_Load
event handler to initialize the IOcrEngine
, so that it becomes as follows:
private void Form1_Load(object sender, EventArgs e)
{
// Initialize Image Viewer object
_viewer = new();
_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:\LEADTOOLS23\Bin\Common\OcrLEADRuntime");
}
Add code inside the OpenToolStripMenuItem_Click
event handler to initialize the IOcrPage
after an image is loaded in the viewer:
private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
using (RasterCodecs codecs = new RasterCodecs())
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = @"C:\LEADTOOLS23\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());
}
}
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
.
Double-click the Save as Searchable PDF
menu item to edit its event handler. Add the following code in it:
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:\LEADTOOLS23\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 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).
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.