How to Read Text from Image (OCR) in C# with IronOCR Tesseract

Photo by Danial Igdery

Originally Posted On: C# OCR – Image to Text – C# Tesseract | Iron OCR (ironsoftware.com)

Reading Text from Images in .Net Applications

In this tutorial, we will learn how to convert images to text in C# and other .NET languages.

We will use the IronOcr.IronTesseract class to recognize text within images and look at the nuances of how to use Iron Tesseract OCR to get the highest performance in terms of accuracy and speed when reading text from images in .NET

To achieve “Image to Text” we will install the IronOCR library into a Visual Studio project.

To do this, we download the IronOcr DLL or use Nuggets .

PM > Install-Package IronOcr

Why IronOcr

We use IronOcr for Tesseract management because its us unique in that it:

  • Works straight out of the box in pure .Net
  • Doesn’t require Tesseract to be installed on your machine.
  • Runs the latest engines: Tesseract 5 ( as well as Tesseract 4 & 3)
  • Is available for any .Net project: .Net Framework 4.5 +, .Net Standard 2 + and .Net Core 2, 3 & 5!
  • Has improved accuracy over and speed over traditional Tesseract
  • Supports Xamarin, Mono, Azure and Docker
  • Manage the complex Tesseract dictionary system using Nuget packages
  • Supports PDFS, MultiFrame Tiffs and all major image formats without configuration
  • Can correct low quality and skewed scans to get the best results from tesseract.

Video Link

Using Tesseract in C#

In this simple example, you can see we use the IronOcr.IronTesseract class to read the text from an image and automatically return its value as a string.

  1. // PM> Install-Package IronOcr
  2. using IronOcr;
  3. var Result = new IronTesseract().Read(@”imgScreenshot.png”);
  4. Console.WriteLine(Result.Text);

Copy code to clipboardVB  C#

Which results in 100% accuracy with the following text:

Iron OCR Simple Example In this simple example we will test the accuracy of our C# OCR library to read text from a PNG Image. This is a very basic test, but things will get more complicated as the tutorial continues. The quick brown fox jumps over the lazy dog

Although this may seem simplistic, there is sophisticated behavior going on ‘under the surface’: scanning the image for alignment, quality and resolution, looking at its properties, optimizing the OCR engine, and using a trained artificial intelligence network to then read the text as a human would.

OCR is not a simple process for a computer to achieve, and reading speeds may be similar to those of a human. In other words, OCR is not an instantaneous process. In this case though, it is 100% accurate.

Advanced Use of Iron OCR Tesseract for C#

In most real world use cases, developers are going to want the best performance possible for their project. In this case, we recommend that you move forward to use the OcrInput and IronTesseract classes within the IronOcr namespace.

OcrInput gives you the facility to set the specific characteristics of an OCR job, such as:

  • Working with almost any type of image including JPEG, TIFF, GIF, BMP & PNG
  • Importing whole or parts of PDF documents
  • Enhancing contrast, resolution & size
  • Correcting for rotation, scan noise, digital noise, skew, negative images

IronTesseract

  • Pick from hundreds of prepackaged language and language variants
  • Use Tesseract 5, 4 or 3 OCR engines “out-of-the-box”
  • Specify a document type whether we are looking at a screenshot, a snippet, or an entire document
  • Read Barcodes
  • Output results to: Searchable PDFs, Hocr HTML , a DOM & Strings

Example: Getting Started with OcrInput + IronTesseract

This all may seem daunting, but in the example below you will see the default settings which we would recommend you start with, which will work with almost any image you input to Iron OCR.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var Input = new OcrInput(@”imgPotter.tiff”)) {
  4. var Result = Ocr.Read(Input);
  5. Console.WriteLine(Result.Text);
  6. }

Copy code to clipboardVB  C#

We can use this even on a medium quality scan with 100% accuracy.

C# OCR Scan From Tiff Example

As you can see, reading the text (and optionally barcodes) from a scanned image such as a TIFF was rather easy. This OCR job yields an accuracy of 100%.

OCR is not a perfect science when it comes to real world documents, yet IronTesseract is about as good as it gets.

You will also note that Iron OCR can automatically read multi-page documents, such as TIFFs and even extract text from PDF documents automatically.

Example: A Low Quality Scan

C# OCR Low Resolution Scan with Digital Noise

Now we will try a much lower quality scan of the same page, at a low DPI, which has lots of distortion and digital noise and damage to the original paper.

This is where IronOCR truly shines against other OCR libraries such as Tesseract, and we will find alternative OCR projects shy away from discussing. OCR on real world scanned images rather than unrealistically ‘perfect’ test cases created digitally to give a 100% OCR accuracy.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var Input = new OcrInput(@”imgPotter.LowQuality.tiff”))
  4. {
  5. Input.Deskew(); // removes rotation and perspective
  6. var Result = Ocr.Read(Input);
  7. Console.WriteLine(Result.Text);
  8. }

Copy code to clipboardVB  C#

Without adding Input.Deskew() to straighten the image we get a 52.5% accuracy. Not good enough.

Adding Input.Deskew() brings us to 99.8% accuracy which is almost as accurate as the OCR of a high quality scan.

Image Filters may take a little time to run – but also reduce OCR processing time. It is a fine balance for a developer to get. to know their input documents.

If you are not certain:

  • Input.Deskew() is a safe and very successful filter to use.
  • Secondly try Input.DeNoise() to fix considerable digital noise.

Performance Tuning

The most important factor in the speed of an OCR job is in fact the quality of the input image. The less background noise that is present and the higher the dpi, with a perfect target dpi at about 200 dpi, will cause the fastest and most accurate OCR results.

This is not, however, necessary, as Iron OCR shines at correcting imperfect documents (though this is time-consuming and will cause your OCR jobs to use more CPU cycles).

If possible, choosing input image formats with less digital noise such as TIFF or PNG can also yield faster results than lossy image formats such as JPEG.

Image Filters

The following Image filters can really improve performance:

  • OcrInput.Rotate( double degrees) – Rotates images by a number of degrees clockwise. For anti-clockwise, use negative numbers.
  • OcrInput.Binarize() – This image filter turns every pixel black or white with no middle ground. May Improve OCR performance cases of very low contrast of text to background.
  • OcrInput.ToGrayScale() – This image filter turns every pixel into a shade of grayscale. Unlikely to improve OCR accuracy but may improve speed
  • OcrInput.Contrast() – Increases contrast automatically. This filter often improves OCR speed and accuracy in low contrast scans.
  • OcrInput.DeNoise() – Removes digital noise. This filter should only be used where noise is expected.
  • OcrInput.Invert() – Inverts every color. E.g. White becomes black : black becomes white.
  • OcrInput.Dilate() – Advanced Morphology. Dilation adds pixels to the boundaries of objects in an image. Opposite of Erode
  • OcrInput.Erode() – Advanced Morphology. Erosion removes pixels on object boundariesOpposite of Dilate
  • OcrInput.Deskew() – Rotates an image so it is the right way up and orthogonal. This is very useful for OCR because Tesseract tolerance for skewed scans can be as low as 5 degrees.
  • OcrInput.DeepCleanBackgroundNoise() – Heavy background noise removal. Only use this filter in case extreme document background noise is known, because this filter will also risk reducing OCR accuracy of clean documents, and is very CPU expensive.
  • OcrInput.EnhanceResolution – Enhances the resolution of low quality images. This filter is not often needed because OcrInput.MinimumDPI and OcrInput.TargetDPI will automatically catch and resolve low resolution inputs.

Performance Tuning for Speed

Using Iron Tesseract, we may wish to speed up OCR on higher quality scans.

If optimizing for speed we might start at this position and then turn features back on until the perfect balance is found.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. // Configure for speed
  4. Ocr.Configuration.BlackListCharacters = “~`$#^*_}{][|@”;
  5. Ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
  6. Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
  7. Ocr.Configuration.EngineMode = TesseractEngineMode.LstmOnly;
  8. Ocr.Language = OcrLanguage.EnglishFast;
  9. using (var Input = new OcrInput(@”imgPotter.tiff”))
  10. {
  11. var Result = Ocr.Read(Input);
  12. Console.WriteLine(Result.Text);
  13. }

Copy code to clipboardVB  C#

This result is 99.8% accurate compared to baseline 100% – but 35% faster.

Reading Cropped Regions of Images

As you can see from the following code sample, Iron’s fork of Tesseract OCR is adept at reading specific areas of images.

We may use a System.Drawing.Rectangle to specify, in pixels, the exact area of an image to read.

This can be incredibly useful when we are dealing with a standardized form which is filled out, where only a certain area has text which changes from case to case.

Example: Scanning an Area of a Page

We can use a System.Drawing.Rectangle to specify a region in which we will read a document. The unit of measurement is always pixels.

We will see that this provides speed improvements as well as avoiding reading unnecessary text. In this example we will read a student’s name from a central area of a standardized document.

 

C# OCR Scan From Tiff Example   C# OCR Scan From Tiff Example

 

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var Input = new OcrInput())
  4. {
  5. // a 41% improvement on speed
  6. var ContentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };
  7. Input.AddImage(“img/ComSci.png”, ContentArea);
  8. var Result = Ocr.Read(Input);
  9. Console.WriteLine(Result.Text);
  10. }

Copy code to clipboardVB  C#

This yields a 41% speed increase – and allows us to be specific. This is incredibly useful for .Net OCR scenarios where documents are similar and consistent such as Invoices, Receipts, Checks, Forms, Expense Claims etc.

ContentAreas (OCR cropping) is also supported when reading PDFs.

International Languages

Iron OCR supports 125 international languages via language packs which are distributed as DLLs, which can be downloaded from this website, or also from the NuGet Package Manager for Visual Studio.

We can install them by browsing NuGet (search for “IronOcr.Languages”) or from the OCR language packs page.

Supported languages Include:

  • Afrikaans
  • Amharic Also known as አማርኛ
  • Arabic Also known as العربية
  • ArabicAlphabet Also known as العربية
  • ArmenianAlphabet Also known as Հայերեն
  • Assamese Also known as অসমীয়া
  • Azerbaijani Also known as azərbaycan dili
  • AzerbaijaniCyrillic Also known as azərbaycan dili
  • Belarusian Also known as беларуская мова
  • Bengali Also known as Bangla,বাংলা
  • BengaliAlphabet Also known as Bangla,বাংলা
  • Tibetan Also known as Tibetan Standard, Tibetan, Central ཡིག་
  • Bosnian Also known as bosanski jezik
  • Breton Also known as brezhoneg
  • Bulgarian Also known as български език
  • CanadianAboriginalAlphabet Also known as Canadian First Nations, Indigenous Canadians, Native Canadian, Inuit
  • Catalan Also known as català, valencià
  • Cebuano Also known as Bisaya, Binisaya
  • Czech Also known as čeština, český jazyk
  • CherokeeAlphabet Also known as ᏣᎳᎩ ᎦᏬᏂᎯᏍᏗ, Tsalagi Gawonihisdi
  • ChineseSimplified Also known as 中文 (Zhōngwén), 汉语, 漢語
  • ChineseSimplifiedVertical Also known as 中文 (Zhōngwén), 汉语, 漢語
  • ChineseTraditional Also known as 中文 (Zhōngwén), 汉语, 漢語
  • ChineseTraditionalVertical Also known as 中文 (Zhōngwén), 汉语, 漢語
  • Cherokee Also known as ᏣᎳᎩ ᎦᏬᏂᎯᏍᏗ, Tsalagi Gawonihisdi
  • Corsican Also known as corsu, lingua corsa
  • Welsh Also known as Cymraeg
  • CyrillicAlphabet Also known as Cyrillic scripts
  • Danish Also known as dansk
  • DanishFraktur Also known as dansk
  • German Also known as Deutsch
  • GermanFraktur Also known as Deutsch
  • DevanagariAlphabet Also known as Nagair,देवनागरी
  • Divehi Also known as ދިވެހި
  • Dzongkha Also known as རྫོང་ཁ
  • Greek Also known as ελληνικά
  • English
  • MiddleEnglish Also known as English (1100-1500 AD)
  • Esperanto
  • Estonian Also known as eesti, eesti keel
  • EthiopicAlphabet Also known as Ge’ez,ግዕዝ, Gəʿəz
  • Basque Also known as euskara, euskera
  • Faroese Also known as føroyskt
  • Persian Also known as فارسی
  • Filipino Also known as National Language of the Philippines, Standardized Tagalog
  • Finnish Also known as suomi, suomen kieli
  • Financial Also known as Financial, Numerical and Technical Documents
  • French Also known as français, langue française
  • FrakturAlphabet Also known as Generic Fraktur, Calligraphic hand of the Latin alphabet
  • Frankish Also known as Frenkisk, Old Franconian
  • MiddleFrench Also known as Moyen Français,Middle French (ca. 1400-1600 AD)
  • WesternFrisian Also known as Frysk
  • GeorgianAlphabet Also known as ქართული
  • ScottishGaelic Also known as Gàidhlig
  • Irish Also known as Gaeilge
  • Galician Also known as galego
  • AncientGreek Also known as Ἑλληνική
  • GreekAlphabet Also known as ελληνικά
  • Gujarati Also known as ગુજરાતી
  • GujaratiAlphabet Also known as ગુજરાતી
  • GurmukhiAlphabet Also known as Gurmukhī, ਗੁਰਮੁਖੀ, Shahmukhi, گُرمُکھی‎, Sihk Script
  • HangulAlphabet Also known as Korean Alphabet,한글,Hangeul,조선글,hosŏn’gŭl
  • HangulVerticalAlphabet Also known as Korean Alphabet,한글,Hangeul,조선글,hosŏn’gŭl
  • HanSimplifiedAlphabet Also known as Samhan ,한어, 韓語
  • HanSimplifiedVerticalAlphabet Also known as Samhan ,한어, 韓語
  • HanTraditionalAlphabet Also known as Samhan ,한어, 韓語
  • HanTraditionalVerticalAlphabet Also known as Samhan ,한어, 韓語
  • Haitian Also known as Kreyòl ayisyen
  • Hebrew Also known as עברית
  • HebrewAlphabet Also known as עברית
  • Hindi Also known as हिन्दी, हिंदी
  • Croatian Also known as hrvatski jezik
  • Hungarian Also known as magyar
  • Armenian Also known as Հայերեն
  • Inuktitut Also known as ᐃᓄᒃᑎᑐᑦ
  • Indonesian Also known as Bahasa Indonesia
  • Icelandic Also known as Íslenska
  • Italian Also known as italiano
  • ItalianOld Also known as italiano
  • JapaneseAlphabet Also known as 日本語 (にほんご)
  • JapaneseVerticalAlphabet Also known as 日本語 (にほんご)
  • Javanese Also known as basa Jawa
  • Japanese Also known as 日本語 (にほんご)
  • JapaneseVertical Also known as 日本語 (にほんご)
  • Kannada Also known as ಕನ್ನಡ
  • KannadaAlphabet Also known as ಕನ್ನಡ
  • Georgian Also known as ქართული
  • GeorgianOld Also known as ქართული
  • Kazakh Also known as қазақ тілі
  • Khmer Also known as ខ្មែរ, ខេមរភាសា, ភាសាខ្មែរ
  • KhmerAlphabet Also known as ខ្មែរ, ខេមរភាសា, ភាសាខ្មែរ
  • Kyrgyz Also known as Кыргызча, Кыргыз тили
  • NorthernKurdish Also known as Kurmanji, کورمانجی ,Kurmancî‎
  • Korean Also known as 한국어 (韓國語), 조선어 (朝鮮語)
  • KoreanVertical Also known as 한국어 (韓國語), 조선어 (朝鮮語)
  • Lao Also known as ພາສາລາວ
  • LaoAlphabet Also known as ພາສາລາວ
  • Latin Also known as latine, lingua latina
  • LatinAlphabet Also known as latine, lingua latina
  • Latvian Also known as latviešu valoda
  • Lithuanian Also known as lietuvių kalba
  • Luxembourgish Also known as Lëtzebuergesch
  • Malayalam Also known as മലയാളം
  • MalayalamAlphabet Also known as മലയാളം
  • Marathi Also known as मराठी
  • MICR Also known as Magnetic Ink Character Recognition, MICR Cheque Encoding
  • Macedonian Also known as македонски јазик
  • Maltese Also known as Malti
  • Mongolian Also known as монгол
  • Maori Also known as te reo Māori
  • Malay Also known as bahasa Melayu, بهاس ملايو‎
  • Myanmar Also known as Burmese ,ဗမာစာ
  • MyanmarAlphabet Also known as Burmese ,ဗမာစာ
  • Nepali Also known as नेपाली
  • Dutch Also known as Nederlands, Vlaams
  • Norwegian Also known as Norsk
  • Occitan Also known as occitan, lenga d’òc
  • Oriya Also known as ଓଡ଼ିଆ
  • OriyaAlphabet Also known as ଓଡ଼ିଆ
  • Panjabi Also known as ਪੰਜਾਬੀ, پنجابی‎
  • Polish Also known as język polski, polszczyzna
  • Portuguese Also known as português
  • Pashto Also known as پښتو
  • Quechua Also known as Runa Simi, Kichwa
  • Romanian Also known as limba română
  • Russian Also known as русский язык
  • Sanskrit Also known as संस्कृतम्
  • Sinhala Also known as සිංහල
  • SinhalaAlphabet Also known as සිංහල
  • Slovak Also known as slovenčina, slovenský jazyk
  • SlovakFraktur Also known as slovenčina, slovenský jazyk
  • Slovene Also known as slovenski jezik, slovenščina
  • Sindhi Also known as सिन्धी, سنڌي، سندھی‎
  • Spanish Also known as español, castellano
  • SpanishOld Also known as español, castellano
  • Albanian Also known as gjuha shqipe
  • Serbian Also known as српски језик
  • SerbianLatin Also known as српски језик
  • Sundanese Also known as Basa Sunda
  • Swahili Also known as Kiswahili
  • Swedish Also known as Svenska
  • Syriac Also known as Syrian, Syriac Aramaic,ܠܫܢܐ ܣܘܪܝܝܐ‎, Leššānā Suryāyā
  • SyriacAlphabet Also known as Syrian, Syriac Aramaic,ܠܫܢܐ ܣܘܪܝܝܐ‎, Leššānā Suryāyā
  • Tamil Also known as தமிழ்
  • TamilAlphabet Also known as தமிழ்
  • Tatar Also known as татар теле, tatar tele
  • Telugu Also known as తెలుగు
  • TeluguAlphabet Also known as తెలుగు
  • Tajik Also known as тоҷикӣ, toğikī, تاجیکی‎
  • Tagalog Also known as Wikang Tagalog, ᜏᜒᜃᜅ᜔ ᜆᜄᜎᜓᜄ᜔
  • Thai Also known as ไทย
  • ThaanaAlphabet Also known as Taana , Tāna , ތާނަ‎
  • ThaiAlphabet Also known as ไทย
  • TibetanAlphabet Also known as Tibetan Standard, Tibetan, Central ཡིག་
  • Tigrinya Also known as ትግርኛ
  • Tonga Also known as faka Tonga
  • Turkish Also known as Türkçe
  • Uyghur Also known as Uyƣurqə, ئۇيغۇرچە‎
  • Ukrainian Also known as українська мова
  • Urdu Also known as اردو
  • Uzbek Also known as O‘zbek, Ўзбек, أۇزبېك‎
  • UzbekCyrillic Also known as O‘zbek, Ўзбек, أۇزبېك‎
  • Vietnamese Also known as Tiếng Việt
  • VietnameseAlphabet Also known as Tiếng Việt
  • Yiddish Also known as ייִדיש
  • Yoruba Also known as Yorùbá

Example: OCR in Arabic (+ many more)

In the following example, we will show how we can scan an Arabic document.

PM> Install-Package IronOcr.Languages.ArabicC# OCR in Arabic Language

  1. // using IronOcr;
  2. // PM> Install IronOcr.Languages.Arabic
  3. var Ocr = new IronTesseract();
  4. Ocr.Language = OcrLanguage.Arabic;
  5. using (var input = new OcrInput())
  6. {
  7. input.AddImage(“img/arabic.gif”);
  8. // add image filters if needed
  9. // In this case, even thought input is very low quality
  10. // IronTesseract can read what conventional Tesseract cannot.
  11. var Result = Ocr.Read(input);
  12. // Console can’t print Arabic on Windows easily.
  13. // Let’s save to disk instead.
  14. Result.SaveAsTextFile(“arabic.txt”);
  15. }

Copy code to clipboardVB  C#

Example: OCR in more than one language in the same document.

In the following example, we will show how to OCR scan multiple languages to the same document.

This is actually very common, where for example a Chinese document might contain English words and Urls.

PM> Install-Package IronOcr.Languages.ChineseSimplified

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. Ocr.Language = OcrLanguage.ChineseSimplified;
  4. Ocr.AddSecondaryLanguage(OcrLanguage.English);
  5. // We can add any number of languages.
  6. // Optionally add custom tesseract .traineddata files by specifying a file path
  7. using (var input = new OcrInput())
  8. {
  9. input.AddImage(“img/MultiLanguage.jpeg”);
  10. var Result = Ocr.Read(input);
  11. Result.SaveAsTextFile(“MultiLanguage.txt”);
  12. }

Copy code to clipboardVB  C#

Multi Page Documents

IronOcr can combine multiple pages / images into a single OcrResult. This is extremely useful where a document has been made from multiple images. We will see later that this special feature of IronTesseract is extremely useful to produce searchable PDFs and HTML files from OCR inputs.

IronOcr makes it possible to “mix and match” images, TIFF frames and PDF pages into a single OCR input.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var input = new OcrInput())
  4. {
  5. input.AddImage(“image1.jpeg”);
  6. input.AddImage(“image2.png”);
  7. input.AddImage(“image3.gif”);
  8. var Result = Ocr.Read(input);
  9. Console.WriteLine($”{Result.Pages.Count()} Pages”);
  10. // 3 Pages
  11. }

Copy code to clipboardVB  C#

We can also easily OCR every page of a TIFF.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var input = new OcrInput())
  4. {
  5. input.AddMultiFrameTiff(“MultiFrame.Tiff”);
  6. var Result = Ocr.Read(input);
  7. Console.WriteLine(Result.Text);
  8. Console.WriteLine($”{Result.Pages.Count()} Pages”);
  9. // 1 page for every frame (page) in the TIFF
  10. }

Copy code to clipboardVB  C#

  1. var Ocr = new IronTesseract();
  2. using (var input = new OcrInput())
  3. {
  4. input.AddPdf(“example.pdf”,”password”);
  5. // We can also select specific PDF page numbers to OCR
  6. var Result = Ocr.Read(input);
  7. Console.WriteLine(Result.Text);
  8. Console.WriteLine($”{Result.Pages.Count()} Pages”);
  9. // 1 page for every page of the PDF
  10. }

Copy code to clipboardVB  C#

Searchable PDFs

Exporting OCR results as searchable PDFs in C# and VB.Net is a popular feature of IronOCR. This can really help with database population, SEO and PDF usability businesses and governments.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var input = new OcrInput())
  4. {
  5. input.Title = “Quarterly Report”
  6. input.AddImage(“image1.jpeg”);
  7. input.AddImage(“image2.png”);
  8. input.AddImage(“image3.gif”);
  9. var Result = Ocr.Read(input);
  10. Result.SaveAsSearchablePdf(“searchable.pdf”)
  11. }

Copy code to clipboardVB  C#

Another OCR trick is to convert an existing PDF document searchable.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var input = new OcrInput())
  4. {
  5. input.Title = “Pdf Metadata Name”
  6. input.AddPdf(“example.pdf”,”password”);
  7. var Result = Ocr.Read(input);
  8. Result.SaveAsSearchablePdf(“searchable.pdf”)
  9. }

Copy code to clipboardVB  C#

The same applies to converting TIFF documents with 1 or more pages to searchable PDFs using IronTesseract.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var input = new OcrInput())
  4. {
  5. input.Title = “Pdf Title”
  6. input.AddMultiFrameTiff(“example.tiff”)
  7. var Result = Ocr.Read(input);
  8. Result.SaveAsSearchablePdf(“searchable.pdf”)
  9. }

Copy code to clipboardVB  C#

Exporting Hocr HTML

We can similarly export OCR result documents to Hocr HTML. This is an XML document which can be parsed by an XML reader, or marked up into visually appealing HTML.

This allows some degree of PDF to HTML and TIFF to HTML conversion.

  1. using IronOcr;
  2. var Ocr = new IronTesseract();
  3. using (var Input = new OcrInput()) {
  4. input.Title = “Html Title”
  5. // Add more content as required…
  6. //input.AddImage(“image2.jpeg”);
  7. //input.AddPdf(“example.pdf”,”password”);
  8. //input.AddMultiFrameTiff(“example.tiff”)
  9. var Result = Ocr.Read(Input);
  10. Result.SaveAsHocrFile(“hocr.html”);
  11. }

Copy code to clipboardVB  C#

Reading Barcodes in OCR Documents

IronOCR has a unique additional advantage over traditional tesseract in that it also reads barcodes and QR codes;

  1. var Ocr = new IronTesseract();
  2. Ocr.Configuration.ReadBarCodes = true;
  3. using (var input = new OcrInput())
  4. {
  5. input.AddImage(“img/Barcode.png”);
  6. var Result = Ocr.Read(input);
  7. foreach (var Barcode in Result.Barcodes) {
  8. Console.WriteLine(Barcode.Value);
  9. // type and location properties also exposed
  10. }
  11. }

Copy code to clipboardVB  C#

A Detailed Look at Image to Text OCR Results

The last thing we will look at in this tutorial is the OCR results object. When we read OCR, we normally only want the text out, but Iron OCR actually contains a huge amount of information which may be of use to advanced developers.

Within an OCR results object, we have a collection of pages which can be iterated. Within each page, we may find barcodes, power graphs, lines of text, words, and characters.

Each of these objects in fact contains: a location; an X coordinate; a Y coordinate; a width and a height; an image associated with it which can be inspected; a font name; the font size; the direction in which the text is written; the rotation of the text; and the statistical confidence that Iron OCR has for that specific word, line, or paragraph.

In short, this allows developers to be creative and work with OCR data in any way they choose to inspect and export information.

We can also work with and export any element from the .Net OCR Results object such as a paragraph, word or barcode as an Image or BitMap.

  1. using IronOcr;
  2. using System.Drawing; //for image export
  3. // We can delve deep into OCR results as an object model of
  4. // Pages, Barcodes, Paragraphs, Lines, Words and Characters
  5. // This allows us to explore, export and draw OCR content using other APIs/
  6. var Ocr = new IronTesseract();
  7. Ocr.Configuration.EngineMode = TesseractEngineMode.TesseractAndLstm;
  8. Ocr.Configuration.ReadBarCodes = true;
  9. using (var Input = new OcrInput(@”imgPotter.tiff”))
  10. {
  11. OcrResult Result = Ocr.Read(Input);
  12. foreach (var Page in Result.Pages)
  13. {
  14. // Page object
  15. int PageNumber = Page.PageNumber;
  16. string PageText = Page.Text;
  17. int PageWordCount = Page.WordCount;
  18. // null if we don’t set Ocr.Configuration.ReadBarCodes = true;
  19. OcrResult.Barcode[] Barcodes = Page.Barcodes;
  20. System.Drawing.Bitmap PageImage = Page.ToBitmap(Input);
  21. int PageWidth = Page.Width;
  22. int PageHeight = Page.Height;
  23. foreach (var Paragraph in Page.Paragraphs)
  24. {
  25. // Pages -> Paragraphs
  26. int ParagraphNumber = Paragraph.ParagraphNumber;
  27. String ParagraphText = Paragraph.Text;
  28. System.Drawing.Bitmap ParagraphImage = Paragraph.ToBitmap(Input);
  29. int ParagraphX_location = Paragraph.X;
  30. int ParagraphY_location = Paragraph.Y;
  31. int ParagraphWidth = Paragraph.Width;
  32. int ParagraphHeight = Paragraph.Height;
  33. double ParagraphOcrAccuracy = Paragraph.Confidence;
  34. OcrResult.TextFlow paragrapthText_direction = Paragraph.TextDirection;
  35. foreach (var Line in Paragraph.Lines)
  36. {
  37. // Pages -> Paragraphs -> Lines
  38. int LineNumber = Line.LineNumber;
  39. String LineText = Line.Text;
  40. System.Drawing.Bitmap LineImage = Line.ToBitmap(Input); ;
  41. int LineX_location = Line.X;
  42. int LineY_location = Line.Y;
  43. int LineWidth = Line.Width;
  44. int LineHeight = Line.Height;
  45. double LineOcrAccuracy = Line.Confidence;
  46. double LineSkew = Line.BaselineAngle;
  47. double LineOffset = Line.BaselineOffset;
  48. foreach (var Word in Line.Words)
  49. {
  50. // Pages -> Paragraphs -> Lines -> Words
  51. int WordNumber = Word.WordNumber;
  52. String WordText = Word.Text;
  53. System.Drawing.Image WordImage = Word.ToBitmap(Input);
  54. int WordX_location = Word.X;
  55. int WordY_location = Word.Y;
  56. int WordWidth = Word.Width;
  57. int WordHeight = Word.Height;
  58. double WordOcrAccuracy = Word.Confidence;
  59. if (Word.Font != null)
  60. {
  61. // Word.Font is only set when using Tesseract Engine Modes rather than LTSM
  62. String FontName = Word.Font.FontName;
  63. double FontSize = Word.Font.FontSize;
  64. bool IsBold = Word.Font.IsBold;
  65. bool IsFixedWidth = Word.Font.IsFixedWidth;
  66. bool IsItalic = Word.Font.IsItalic;
  67. bool IsSerif = Word.Font.IsSerif;
  68. bool IsUnderLined = Word.Font.IsUnderlined;
  69. bool IsFancy = Word.Font.IsCaligraphic;
  70. }
  71. foreach (var Character in Word.Characters)
  72. {
  73. // Pages -> Paragraphs -> Lines -> Words -> Characters
  74. int CharacterNumber = Character.CharacterNumber;
  75. String CharacterText = Character.Text;
  76. System.Drawing.Bitmap CharacterImage = Character.ToBitmap(Input);
  77. int CharacterX_location = Character.X;
  78. int CharacterY_location = Character.Y;
  79. int CharacterWidth = Character.Width;
  80. int CharacterHeight = Character.Height;
  81. double CharacterOcrAccuracy = Character.Confidence;
  82. // Output alternative symbols choices and their probability.
  83. // Very useful for spell checking
  84. OcrResult.Choice[] Choices = Character.Choices;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }

Copy code to clipboardVB  C#

Summary

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 see that if we input even an imperfect document to IronOCR, it can accurately read its content to a statistical accuracy of about 99%, even though the document was badly formatted, skewed, and had digital noise.

We can also read barcodes in OCR scans, and even export our OCR as HTML and searchable PDFs.

This is unique to Iron OCR and is a feature you will not find in standard OCR libraries or vanilla Tesseract.

Moving Forward

To continue to learn more about Iron OCR, we recommend that you:

Source Code Download

You may also enjoy the other .Net OCR tutorials in this section.

Leave a Reply

Your email address will not be published. Required fields are marked *