Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
OCR stands for Optical Character Recognition. It provides the facility to convert an image file to machine encoded text. The scanned documents are always saved as image file by the computer. The data in these image files cannot be searched, edited, or saved in text format using a normal text editor or even using word processing application. OCR processing helps convert these images to machine readable text for further processing by its users.
In this modern age, the scanned documents shared over int.NET are in digital format and mostly in the form of PDFs or images. There are a bunch of online resources available which converts the image to text. However, mostly businesses require this functionality in their software applications. Keeping this in mind, there are many libraries which provide OCR processing technology to be embedded in software applications.
In this article, we are going to discuss two of the most popular OCR libraries for C#. These are:
IronOCR for .NET is C# library to scan, search and read images & PDFs. It takes an image or PDF file as an input, and uses the latest Tesseract 5 custom build .NET OCR engine to output text, structured data or searchable PDF documents. Its Tesseract is available in 125+ languages along with cross-platform support in .NET Core, Standard, from 2.0 up to 7.
IronOCR is a user-friendly API which allows C# developers to convert images to text automatically, simply by using IronTesseract
class and API key. It prioritizes speed, accuracy and ease of use. It also aids Computer Vision API to find text with trained set of models.
Another powerful feature of IronOCR is that it can scan barcodes & QR codes from all image files and read its text. Other important features of IronOCR are given below.
System.Drawing Objects
, Streams, PDF documents (optimized target DPI)Now, let's have a look at Google Cloud Vision API.
Google Cloud Vision API is a Google Cloud OCR client library which supports C# language. It allows C# developers to easily integrate Computer Vision detection features in software applications. It performs OCR and detects text from image file, image labels, face detection, landmark detection.
Google Cloud Vision API uses REST and RPC APIs to provide a powerful pretrained ML (machine learning) model. With Cloud import vision API, you can quickly classify images to millions of already pre-defined categories. It can also detect objects, read text from printed documents and handwritten text.
The rest of the article goes as follows:
In this tutorial, we are going to use Visual Studio 2022 latest version. So, I assume you have already downloaded and installed it for C#. If not, you can download from Visual Studio website.
Now, we need to create a Console project to get started with both libraries. Follow the steps to create a project:
Click on Create a new Project.
Select C# Console Application from given options.
Click Next.
From additional information, select .NET 6.0 Framework as it is the most stable version.
Next, we will now install the libraries in our project for comparison.
There are multiple ways to install IronOCR library. Let's have a look at them one by one.
NuGet is the package manager for downloading and installing dependencies in your project. Its packages contain the compiled code (DLL) and the manifest file. Access it using the following method:
Click Manage NuGet Packages for Solution
Click Manage NuGet Packages
Now, the NuGet Package Manager window will open. Browse for IronOCR and click Install.
It can be downloaded directly from the NuGet official website. Follow given steps:
Simply visit the Iron Software website and navigate to the IronOCR for .NET webpage. Scroll to the bottom and click Download DLL or Download Windows Installer.
A Zip file will be downloaded. Extract it and add in your project file or run the Windows Installer. Follow below steps to add it to your project.
Select Package Manager Console and type the following command:
Install-Package IronOCR
This will automatically download and install IronOCR in your project.
Now, we are ready to use IronOCR in our project.
There is only one namespace required and needs to be added on top of the source code file where we need to access its functions.
using IronOCR;
using IronOCR;
Imports IronOCR
Now, let's install Google Vision OCR.
To be able to use vision API in your C# project, you must have the following prerequisites fulfilled
You can have a detailed look at the setup and requirements of using Google Cloud Vision from the official documentation here.
Now, to install Google Cloud client library for performing OCR processing in Visual Studio, we need to use NuGet Package Manager.
Access it using the following method:
Click Manage NuGet Packages for Solutions
Click Manage NuGet Packages
Now, the NuGet Package Manager window will open. Browse for Google Cloud Vision OCR and click Install.
Include the following namespace to use Google OCR Vision API:
using Google.Cloud.Vision.V1;
using Google.Cloud.Vision.V1;
Imports Google.Cloud.Vision.V1
Also set the environment variable with the key credentials downloaded in JSON file.
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "key.json");
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "key.json");
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "key.json")
Now, everything is set up and ready to use.
Reading data from images is quite a tedious task. Images resolution and quality play an important role while extracting content. Both the libraries provide Optical Character Recognition (OCR) functionality to extract text from images.
IronOCR makes it very easy for the developers to read the contents of an image file with its powerful IronTesseract
class. Here we will use PNG image to read text from image file and the code is as follows:
var OCR = new IronTesseract();
using (var Input = new OcrInput()){
Input.AddImage("test-files/employmentapp.png");
var Result = OCR.Read(Input);
Console.WriteLine(Result.Text);
}
var OCR = new IronTesseract();
using (var Input = new OcrInput()){
Input.AddImage("test-files/employmentapp.png");
var Result = OCR.Read(Input);
Console.WriteLine(Result.Text);
}
Dim OCR = New IronTesseract()
Using Input = New OcrInput()
Input.AddImage("test-files/employmentapp.png")
Dim Result = OCR.Read(Input)
Console.WriteLine(Result.Text)
End Using
The output of IronOCR matches the original image given to it. The code is clean and easy to understand without any technicalities.
Google Cloud Vision OCR also converts the image to text with different fonts. First, we need to create a client using the credentials file. Then using this client object we can call the DetectText
method to get a response in form of annotation. The code is given as follows:
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "key.json");
var client = ImageAnnotatorClient.Create();
var image = Image.FromFile("test-files/employmentapp.png");
var response = client.DetectText(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
{
Console.WriteLine(annotation.Description);
}
}
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "key.json");
var client = ImageAnnotatorClient.Create();
var image = Image.FromFile("test-files/employmentapp.png");
var response = client.DetectText(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
{
Console.WriteLine(annotation.Description);
}
}
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "key.json")
Dim client = ImageAnnotatorClient.Create()
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile("test-files/employmentapp.png")
Dim response = client.DetectText(image)
For Each annotation In response
If annotation.Description IsNot Nothing Then
Console.WriteLine(annotation.Description)
End If
Next annotation
The same image is given as input in order to compare output of both libraries.
From the above output, you can clearly see that, IronOCR preserves the image output formats. Although Google OCR has given accurate output text, but the formatting of the table is not preserved. IronOCR has preserved the formatting of the table exactly as in given image.
IronOCR provides a unique and useful feature while reading images i.e. it can read barcodes and QR Codes. It can detect barcodes and display its value with ease. First, set the ReadBarCodes
configuration to true and then iterate through each of the barcode in the OCR results. The code for reading barcodes is given below:
var OCR = new IronTesseract();
OCR.Configuration.ReadBarCodes = true;
using (var input = new OcrInput()){
input.AddImage("test-files/Barcode.png");
var Result = OCR.Read(input);
foreach (var Barcode in Result.Barcodes){
Console.WriteLine(Barcode.Value);
}
}
var OCR = new IronTesseract();
OCR.Configuration.ReadBarCodes = true;
using (var input = new OcrInput()){
input.AddImage("test-files/Barcode.png");
var Result = OCR.Read(input);
foreach (var Barcode in Result.Barcodes){
Console.WriteLine(Barcode.Value);
}
}
Dim OCR = New IronTesseract()
OCR.Configuration.ReadBarCodes = True
Using input = New OcrInput()
input.AddImage("test-files/Barcode.png")
Dim Result = OCR.Read(input)
For Each Barcode In Result.Barcodes
Console.WriteLine(Barcode.Value)
Next Barcode
End Using
All the three barcodes in the input are read successfully, and their hidden text is displayed.
Google Vision API does not allow this functionality yet. Reading barcodes can be handy in software applications. However, Google OCR allows you to extract handwritten text and to get text from scanned document as an image file. Code for label detection is as follows:
var client = ImageAnnotatorClient.Create();
var image = Image.FromUri("gs://cloud-samples-data/vision/using_curl/shanghai.jpeg");
var labels = client.DetectLabels(image);
Console.WriteLine("Labels (and confidence score):");
Console.WriteLine(new String('=', 30));
foreach (var label in labels)
{
Console.WriteLine($"{label.Description} ({(int)(label.Score * 100)}%)");
}
var client = ImageAnnotatorClient.Create();
var image = Image.FromUri("gs://cloud-samples-data/vision/using_curl/shanghai.jpeg");
var labels = client.DetectLabels(image);
Console.WriteLine("Labels (and confidence score):");
Console.WriteLine(new String('=', 30));
foreach (var label in labels)
{
Console.WriteLine($"{label.Description} ({(int)(label.Score * 100)}%)");
}
Imports System
Dim client = ImageAnnotatorClient.Create()
Dim image = System.Drawing.Image.FromUri("gs://cloud-samples-data/vision/using_curl/shanghai.jpeg")
Dim labels = client.DetectLabels(image)
Console.WriteLine("Labels (and confidence score):")
Console.WriteLine(New String("="c, 30))
For Each label In labels
Console.WriteLine($"{label.Description} ({CInt(Math.Truncate(label.Score * 100))}%)")
Next label
Reading PDF files is as easy as reading image files in IronOCR. You only need to change the AddImage
method to AddPdf
in the image reading code. The code goes as follows:
var OCR = new IronTesseract();
using (var Input = new OcrInput()){
Input.AddPdf("test-files/example.PDF");
var Result = OCR.Read(Input);
Console.WriteLine(Result.Text);
}
var OCR = new IronTesseract();
using (var Input = new OcrInput()){
Input.AddPdf("test-files/example.PDF");
var Result = OCR.Read(Input);
Console.WriteLine(Result.Text);
}
Dim OCR = New IronTesseract()
Using Input = New OcrInput()
Input.AddPdf("test-files/example.PDF")
Dim Result = OCR.Read(Input)
Console.WriteLine(Result.Text)
End Using
The extracted text is in the same formatting as the PDF file.
Google OCR also provides facility to extract text from a PDF/TIFF documents. However, it only detects text if the file is at Google Cloud Storage. For this you need to create a Google Storage bucket. Code for C# is not straightforward and quite lengthy to implement here. There are no samples available for C# to detect texts in files. You can have a look at Java code as a reference to give it a try.
IronOCR is free for development purposes, but it needs to be licensed for commercial use. It also provides a free trial to test all its potential for your needs. The Lite package starts from $749 with a free trial. IronOCR provides 1 year of product support and updates for free and then $399 year after. All licenses are perpetual, meaning only one time purchase and no hidden charges. You can also choose royalty free redistribution coverage for SaaS and OEM products in just $1999 single time purchase. For more info on license packages and pricing plans please visit here.
Google Cloud Vision pricing plans are based on number of operations performed by the application on an image. For files like PDF which have multiple pages, each page is treated as an image. Moreover, each feature applied to an image is a separate billable unit. E.g. if you apply text detection and label detection to the same image, each feature will be charged separately. The pricing plans are given below and for more info, please visit this link.
IronOCR provides C# developers the most advanced Tesseract API we know of, on any platform. IronOCR can be deployed on Windows, Linux, Mac, Azure, AWS, Lambda and supports .NET Framework projects as well as .NET Standard and .NET Core_. We can also read barcodes in OCR scans, and even export our OCR as HTML and searchable PDFs.
Google Cloud Vision API is an advanced AI built API. It provides a variety of image analyzing features which can be very helpful in building ML applications. It allows developers to directly communicate with the Google Cloud using an API key, which means there is no need to store files locally.
IronOCR licenses are user-based, which means you should always purchase a license based on the number of developers who will use the product. Google Cloud Vision licenses are based on the number of pictures to extract information from and analyze the data. The licenses are on monthly basis and the prices become very high for large number of images as compared to IronOCR license. Moreover, IronOCR license is one-time purchase, and it can be used for lifetime, and it supports OEM and SaaS distribution.
In overall conclusion, both the libraries possess machine learning capabilities. IronOCR has a slight advantage over Google OCR as it is specifically built for C# .NET framework which is fast and time saving. It provides all features with very few lines of code, lifting the burden from developer to write long lengthy codes. It is built on most popular Tesseract 5 API which makes it easy to integrate and analyze images, and other file formats with accurate output. On the other hand, Google Vision OCR is built on AI and more focused for Java, Python and REST and can only run when connected to Google Cloud. This can be time-consuming as the response comes from server side. You can choose the library according to your specific needs.
Now, you can get 5 Iron products for the price of 2. Following tools are included in Iron Suite:
Visit this link to explore more.
You can download IronOCR from here.
9 .NET API products for your office documents