Optical Character Recognition in PDF Using Tesseract Open-Source Engine
Tags: csharp, beginners, productivity, tutorial
Published: 2019-06-10 11:20:10

Optical character recognition (OCR) is a technology used to convert scanned paper documents, in the form of PDF files or images, to searchable, editable data. Paper documents—such as brochures, invoices, contracts, etc.—are sent via email. This process usually involves a scanner that converts the document to lots of different colors, known as a raster image. In order to extract the data and repurpose the content of the document, an OCR engine is necessary. The OCR engine detects the characters present in the image, puts those characters into words, and then into sentences, enabling you to search and edit the content of the document.
<h2>Tesseract engine</h2>
<strong><a href="https://github.com/tesseract-ocr/">Tesseract</a></strong> is an optical character recognition engine, one of the most accurate OCR engines currently available. It is licensed under <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0</a> and has been developed by Google since 2006.
<h2>Getting Started with Essential PDF and Tesseract Engine</h2>
<a href="https://www.syncfusion.com/pdf-framework/net">Syncfusion Essential PDF</a> supports OCR by using the Tesseract open-source engine. With a few lines of code, a scanned paper document containing raster images is converted to a searchable and selectable document.
<p class="MsoNormal" style="margin-bottom: 7.5pt; line-height: 1.2;">You can download the OCR processor product setup <a href="https://www.syncfusion.com/downloads/latest-version">here</a>.</p>

<h3>Deployment Requirements</h3>
The following assemblies are required to deploy Essential PDF and the OCR process.
<p style="margin-bottom: 10px;"><strong><em>Syncfusion assemblies</em></strong></p>

<ul>
 	<li>Syncfusion.Compression.Base.dll</li>
 	<li>Syncfusion.Pdf.Base.dll</li>
 	<li>Syncfusion.OcrProcessor.Base.dll</li>
</ul>
<p style="margin-bottom: 10px;"><strong><em>Tesseract assemblies</em></strong></p>

<ul>
 	<li>SyncfusionTessaract.dll (Tesseract Engine Version 3.02)</li>
 	<li>liblept168.dll (<a href="http://www.leptonica.com/">Leptonica</a> image processing library used by Tesseract engine)</li>
</ul>
<h3>Referencing OCR assemblies in a .NET project</h3>
<p style="margin-bottom: 20px;">To reference the OCR assemblies in a .NET project:</p>

<ol>
 	<li style="margin-bottom: 10px;">Open the <strong>Solution Explorer</strong> of the application you have created. Right-click the <strong>Reference</strong> folder and then click <strong>Add References</strong>.</li>
 	<li>Add the following assemblies as references in the application:
<ul style="margin-bottom: 10px;">
 	<li>Syncfusion.Compression.Base.dll</li>
 	<li>Syncfusion.Pdf.Base.dll</li>
 	<li>Syncfusion.OcrProcessor.Base.dll</li>
</ul>
</li>
 	<li><strong>SyncfusionTessaract.dll </strong>and <strong>liblept168.dll</strong> should not be added as a reference. They should be kept in the local machine, and the location of the assemblies should be passed as a parameter to the OCR processor.</li>
</ol>
<h3>Performing OCR for a scanned paper document</h3>
<p style="padding-left: 30px;">1. To perform optical character recognition, as a first step, create the <strong>OCR</strong> processor by generating an object of the <strong>OCRProcessor</strong> class. It is mandatory for the constructor of the <strong>OCRProcessor</strong> class to accept the path of the <strong>Tesseract</strong> binaries, <strong>SyncfusionTessaract.dll</strong>, and<strong> liblept168.dll</strong>.</p>
```csharp
//Initializes the OCR processor by providing Tesseract binaries(SyncfusionTesseract.dll and liblept168.dll)
//to the OCR processor overload.
OCRProcessor processor = new OCRProcessor(@"TesseractBinaries");
```
<p style="padding-left: 30px;">2. The PDF document that has to undergo the optical character recognition is loaded by using the <strong>PdfLoadedDocument </strong>class.</p>
```csharp
//Loads a PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
```
<p style="padding-left: 30px;">3. The next step is to set the language for the OCR process and start the OCR process with the input of the language dictionary. Tesseract supports a variety of languages. The following code explains the OCR process for English and how to provide the English dictionary input.</p>
```csharp
//Sets OCR language to process.
processor.Settings.Language = "eng";
//Processes OCR by providing PDF document, data dictionary, and language.
processor.PerformOCR(loadedDocument, @"Tessdata");
```
<strong><em>Note</em></strong><em>: </em>You can get the Tesseract binaries <span style="font-weight: bold;">SyncfusionTessaract.dll, liblept168.dll</span>, and the language pack (<span style="font-weight: bold;">tessdata</span>)— by downloading the OCR processor zip file from the following location: <span style="line-height: 107%;"><a href="https://www.syncfusion.com/downloads/latest-version">https://www.syncfusion.com/downloads/latest-version</a></span>

<p style="padding-left: 30px;">4. The final step is to save the PDF document and dispose of the <strong>PdfLoadedDocument</strong> object. The saved PDF document now contains the contents in a searchable form.</p>
```csharp
//Saves the OCR-processed PDF document to a disk.
loadedDocument.Save("Sample.pdf");
loadedDocument.Close(true);
```
<h3>Performing OCR on a section of the document</h3>
Optical character recognition can also be performed on a section of a document rather than the complete document. The following documentation <span style="line-height: 107%;"><a href="https://help.syncfusion.com/file-formats/pdf/working-with-ocr#performing-ocr-for-a-region-of-the-document">link</a></span> provides a code sample and explanation.
<h2>Multiple language support for OCR</h2>
The Tesseract engine, starting from version 3, supports a variety of languages such as Arabic, English, Bulgarian, Catalan, Czech, Chinese and German as given in the following table.

Essential PDF also supports all these languages in the OCR processor. By default, Syncfusion ships only the English dictionary in the package. The dictionary packs for the other languages can be downloaded from the following online location:

<a style="background-color: #ffffff;" href="https://github.com/tesseract-ocr/tessdata" target="_blank" rel="noopener">https://github.com/tesseract-ocr/tessdata</a>
<ol>
 	<li>The dictionary packs from the above link can be downloaded, extracted to a folder, and the location of the folder can be passed to the <strong>PerformOCR()</strong> method of the <strong>OCRProcessor</strong> class.</li>
 	<li>It is also mandatory to change the corresponding language code in the <strong>OCRProcessor.Settings.Language</strong> property. For example, to perform optical character recognition in German, the property should be set as <strong>processor.Settings.Language = "deu";</strong></li>
</ol>
The following table shows the complete set of supported languages and their language codes.
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="50%">
<p align="center"><strong>Language</strong></p>
</td>
<td valign="top" width="50%">
<p align="center"><strong>Language code</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="50%">Arabic</td>
<td valign="top" width="50%">ara</td>
</tr>
<tr>
<td valign="top" width="50%">Azerbaijani</td>
<td valign="top" width="50%">aze</td>
</tr>
<tr>
<td valign="top" width="50%">Bulgarian</td>
<td valign="top" width="50%">bul</td>
</tr>
<tr>
<td valign="top" width="50%">Catalan</td>
<td valign="top" width="50%">cat</td>
</tr>
<tr>
<td valign="top" width="50%">Czech</td>
<td valign="top" width="50%">ces</td>
</tr>
<tr>
<td valign="top" width="50%">Simplified Chinese</td>
<td valign="top" width="50%">chi_sim</td>
</tr>
<tr>
<td valign="top" width="50%">Traditional Chinese</td>
<td valign="top" width="50%">chi_tra</td>
</tr>
<tr>
<td valign="top" width="50%">Cherokee</td>
<td valign="top" width="50%">chr</td>
</tr>
<tr>
<td valign="top" width="50%">Danish</td>
<td valign="top" width="50%">dan</td>
</tr>
<tr>
<td valign="top" width="50%">Danish (Fraktur)</td>
<td valign="top" width="50%">dan-frak</td>
</tr>
<tr>
<td valign="top" width="50%">German, standard and Fraktur script</td>
<td valign="top" width="50%">deu</td>
</tr>
<tr>
<td valign="top" width="50%">Greek</td>
<td valign="top" width="50%">ell</td>
</tr>
<tr>
<td valign="top" width="50%">English</td>
<td valign="top" width="50%">eng</td>
</tr>
<tr>
<td valign="top" width="50%">Old English</td>
<td valign="top" width="50%">enm</td>
</tr>
<tr>
<td valign="top" width="50%">Esperanto</td>
<td valign="top" width="50%">epo</td>
</tr>
<tr>
<td valign="top" width="50%">Estonian</td>
<td valign="top" width="50%">est</td>
</tr>
<tr>
<td valign="top" width="50%">Finnish</td>
<td valign="top" width="50%">fin</td>
</tr>
<tr>
<td valign="top" width="50%">French</td>
<td valign="top" width="50%">fra</td>
</tr>
<tr>
<td valign="top" width="50%">Old French</td>
<td valign="top" width="50%">frm</td>
</tr>
<tr>
<td valign="top" width="50%">Galician</td>
<td valign="top" width="50%">glg</td>
</tr>
<tr>
<td valign="top" width="50%">Hebrew</td>
<td valign="top" width="50%">heb</td>
</tr>
<tr>
<td valign="top" width="50%">Hindi</td>
<td valign="top" width="50%">hin</td>
</tr>
<tr>
<td valign="top" width="50%">Croatian</td>
<td valign="top" width="50%">hrv</td>
</tr>
<tr>
<td valign="top" width="50%">Hungarian</td>
<td valign="top" width="50%">hun</td>
</tr>
<tr>
<td valign="top" width="50%">Indonesian</td>
<td valign="top" width="50%">ind</td>
</tr>
<tr>
<td valign="top" width="50%">Italian</td>
<td valign="top" width="50%">ita</td>
</tr>
<tr>
<td valign="top" width="50%">Japanese</td>
<td valign="top" width="50%">jpn</td>
</tr>
<tr>
<td valign="top" width="50%">Korean</td>
<td valign="top" width="50%">kor</td>
</tr>
<tr>
<td valign="top" width="50%">Latvian</td>
<td valign="top" width="50%">lav</td>
</tr>
<tr>
<td valign="top" width="50%">Lithuanian</td>
<td valign="top" width="50%">lit</td>
</tr>
<tr>
<td valign="top" width="50%">Dutch</td>
<td valign="top" width="50%">nld</td>
</tr>
<tr>
<td valign="top" width="50%">Norwegian</td>
<td valign="top" width="50%">nor</td>
</tr>
<tr>
<td valign="top" width="50%">Polish</td>
<td valign="top" width="50%">pol</td>
</tr>
<tr>
<td valign="top" width="50%">Portuguese</td>
<td valign="top" width="50%">por</td>
</tr>
<tr>
<td valign="top" width="50%">Romanian</td>
<td valign="top" width="50%">ron</td>
</tr>
<tr>
<td valign="top" width="50%">Russian</td>
<td valign="top" width="50%">rus</td>
</tr>
<tr>
<td valign="top" width="50%">Slovakian</td>
<td valign="top" width="50%">slk</td>
</tr>
<tr>
<td valign="top" width="50%">Slovenian</td>
<td valign="top" width="50%">slv</td>
</tr>
<tr>
<td valign="top" width="50%">Albanian</td>
<td valign="top" width="50%">sqi</td>
</tr>
<tr>
<td valign="top" width="50%">Spanish</td>
<td valign="top" width="50%">spa</td>
</tr>
<tr>
<td valign="top" width="50%">Serbian</td>
<td valign="top" width="50%">srp</td>
</tr>
<tr>
<td valign="top" width="50%">Swedish</td>
<td valign="top" width="50%">swe</td>
</tr>
<tr>
<td valign="top" width="50%">Tamil</td>
<td valign="top" width="50%">tam</td>
</tr>
<tr>
<td valign="top" width="50%">Telugu</td>
<td valign="top" width="50%">tel</td>
</tr>
<tr>
<td valign="top" width="50%">Tagalog</td>
<td valign="top" width="50%">tgl</td>
</tr>
<tr>
<td valign="top" width="50%">Thai</td>
<td valign="top" width="50%">tha</td>
</tr>
<tr>
<td valign="top" width="50%">Turkish</td>
<td valign="top" width="50%">tur</td>
</tr>
<tr>
<td valign="top" width="50%">Ukrainian</td>
<td valign="top" width="50%">ukr</td>
</tr>
<tr>
<td valign="top" width="50%">Vietnamese</td>
<td valign="top" width="50%">vie</td>
</tr>
</tbody>
</table>
<h2>Tips for improving OCR accuracy</h2>
You can improve the accuracy of the OCR process by choosing the correct compression method when converting the scanned paper to a TIFF image and then to a PDF document:
<ul>
 	<li>Tesseract works best with text when at least 300 dots per inch (DPI) are used, so it is beneficial to resize images.</li>
 	<li>Compression:
<ul style="margin-bottom: 5px;">
 	<li>Use (zip) lossless compression for color or gray-scale images.</li>
 	<li>Use CCITT Group 4 or JBIG2 (lossless) compression for monochrome images. This ensures that optical character recognition works on the highest-quality image, thereby improving the OCR accuracy. This is especially useful in low-resolution scans.</li>
</ul>
</li>
 	<li>In addition, rotated images and skewed images can also affect the accuracy and readability of the OCR process.</li>
</ul>
<p style="margin-bottom: 20px;">For more details regarding quality improvement, refer to the following link:</p>
<p style="margin-bottom: 20px;"><a href="https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality." target="_blank" rel="noopener">https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality.</a></p>
The sample can be checked out from the <a href="https://github.com/SyncfusionExamples/optical-character-recognition-in-pdf">GitHub repository</a>. Give it a star, if it is useful. 
<p style="margin-bottom: 20px;">Take a moment to peruse the <a href="https://help.syncfusion.com/file-formats/pdf/working-with-ocr">documentation</a>, where you’ll find other options and features, all accompanying code examples.</p>
<p style="margin-bottom: 20px;">If you are new to our <a href="https://www.syncfusion.com/pdf-framework/net" target="_blank" rel="noopener">PDF library</a>, it is highly recommended that you follow our <a href="https://help.syncfusion.com/file-formats/pdf/getting-started">Getting Started guide</a>.</p>
<p style="margin-bottom: 20px;">If you have any questions or require clarification for these features, please let us know in the comments below. You can also contact us through our <a href="https://www.syncfusion.com/forums">support forum</a> or <a href="https://www.syncfusion.com/support/directtrac/">Direct-Trac</a>. We are happy to assist you!</p>
<span style="font-style: italic;">If you like this blog post, we think you'll also like the following resources:</span>
<ul>
 	<li>[Ebook]<span style="font-style: italic;"><a href="https://www.syncfusion.com/ebooks/csharp" target="_blank" rel="noopener">C# Succinctly</a></span></li>
 	<li>[Ebook]<span style="font-style: italic;"><a href="https://www.syncfusion.com/ebooks/pdf" target="_blank" rel="noopener">PDF Succinctly</a></span></li>
 	<li>[Ebook]<span style="font-style: italic;"><a href="https://www.syncfusion.com/ebooks/webservers" target="_blank" rel="noopener">Web Servers Succinctly</a></span></li>
 	<li>[Blog post] <a href="https://blog.syncfusion.com/post/7-ways-to-compress-pdf-files.aspx">7 ways to compress PDF files in C#, VB.NET</a></li>
</ul>

The post [Optical Character Recognition in PDF using Tesseract Opent Source Engine](https://blog.syncfusion.com/post/optical-character-recognition-in-pdf-using-tesseract-open-source-engine.aspx) appeared first on [Syncfusion Blogs](https://blog.syncfusion.com).