# OCR Usage Basics This article explains how to use OCR. GroupDocs.Parser for .NET enables text recognition (OCR) in image files and PDF files composed of images. It should be noted that not all languages ​​represented by the [Language](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/language/) class are currently supported for recognition without implicitly downloading additional resources from the internet. However, if internet access is available, all necessary resources will be downloaded implicitly when selecting any recognition language. Currently supported languages ​​without additional downloads: English, Chinese, Japanese, Korean, Arabic. [GetText](https://reference.groupdocs.com/parser/net/groupdocs.parser/parser/gettext#gettext_1) and [GetTextAreas](https://reference.groupdocs.com/parser/net/groupdocs.parser/parser/gettextareas#gettextareas_1) methods from [Parser](https://reference.groupdocs.com/parser/net/groupdocs.parser/parser/) class are used to extract text from images. To extract text from image files or non-text PDF documents GetText method is used: * Instantiate [Parser](https://reference.groupdocs.com/parser/net/groupdocs.parser/parser/) object; * Instantiate [TextOptions](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/textoptions/) object with useOcr = true; * Call [GetText](https://reference.groupdocs.com/parser/net/groupdocs.parser/parser/gettext#gettext_1)(TextOptions) method with TextOptions parameter and obtain TextReader object; * Check if the reader isn’t null (text extraction is supported for the document); * Read text from the reader. The following example shows how to extract text from an image file: ```C# // Create an instance of Parser class using (Parser parser = new Parser(Constants.SampleScan)) { // Set OCR options TextOptions options = new TextOptions(false, true); options.OcrOptions = new OcrOptions(); options.OcrOptions.Language = Language.Jpn; options.OcrOptions.PagePreviewOptions = new PagePreviewOptions(); options.OcrOptions.PagePreviewOptions.Dpi = 144; // Extract text using OCR using (TextReader reader = parser.GetText(options)) { // Print text or 'not supported' message Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd()); } } ``` To extract text areas from image files or non-text PDF documents GetTextAreas method is used: * Instantiate [Parser](https://reference.groupdocs.com/parser/net/groupdocs.parser/parser/) object; * Instantiate [PageTextAreaOptions](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/pagetextareaoptions) object with useOcr = true; * Call [GetTextAreas](https://reference.groupdocs.com/parser/net/groupdocs.parser/parser/gettextareas/#gettextareas_1)(PageTextAreaOptions) method and obtain the collection of [PageTextArea](https://reference.groupdocs.com/parser/net/groupdocs.parser.data/pagetextarea) objects; * Check if the collection isn’t null (text areas extraction is supported for the document); * Iterate through the collection and get rectangles and texts. The following example shows how to extract text areas from the image file: ```C# // Create an instance of Parser class using (Parser parser = new Parser(Constants.SampleScan)) { // Create an instance of PageTextAreaOptions to use OCR PageTextAreaOptions options = new PageTextAreaOptions(true); // Extract text areas IEnumerable areas = parser.GetTextAreas(options); // Check if text areas extraction is supported if (areas == null) { Console.WriteLine("Text areas extraction isn't supported"); return; } // Iterate over text areas foreach (PageTextArea a in areas) { // Print text, position and size for each text area Console.WriteLine(a.Text); Console.WriteLine("\tPosition: ({0}; {1})", a.Rectangle.Left, a.Rectangle.Top); Console.WriteLine("\tSize: ({0}; {1})", a.Rectangle.Size.Width, a.Rectangle.Size.Height); } } ``` [TextOptions](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/textoptions/) and [PageTextAreaOptions](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/pagetextareaoptions) classes have the property OcrOptions. OcrOptions class has the following members: | Member | Description | | --- | --- | | Rectangle | Is used to pass a rectangular area to restrict the area of the text recognition. | | Handler | An instance of [OcrEventHandler](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocreventhandler) class to handle any warnings which occur while the text recognition. | The following sections describe how to use this property. ## How to restrict the area of the text recognition To restrict an area of the image for the text recognition [OcrOptions](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocroptions) class is used. Set [Rectangle](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocroptions/rectangle) property to restrict the rectangular area for the text recognition. The following example shows how to restrict the text recognition by the rectangular area: ```C# // Create an instance of Parser class using (Parser parser = new Parser(Constants.SampleScan)) { // Create an instance of OcrOptions to set a rectangle OcrOptions ocrOptions = new OcrOptions(new Data.Rectangle(0, 0, 400, 200)); // Create an instance of TextOptions to use OCR TextOptions options = new TextOptions(false, true, ocrOptions); // Extract text using OCR using (TextReader reader = parser.GetText(options)) { // Print text or 'not supported' message Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd()); } } ``` ## How to handle warnings To restrict an area of the image for the text recognition [OcrOptions](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocroptions) class is used. Set [Handler](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocroptions/handler) property to handle warning messages. [HasWarnings](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocreventhandler/haswarnings) property of [OcrEventHandler](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocreventhandler) class is used to indicate if any warnings occur. Use [Warnings](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocreventhandler/warnings) to get all warnings or [GetWarnings](https://reference.groupdocs.com/parser/net/groupdocs.parser.options/ocreventhandler/getwarnings) method for warnings for the page. the empty list returns if no warning occurs during the text recognition. The following example shows how to handle warning messages: ```C# // Create an instance of Parser class using (Parser parser = new Parser(Constants.SampleScan)) { // Create an instance of OcrEventHandler to handle warnings OcrEventHandler handler = new OcrEventHandler(); // Create an instance of OcrOptions to set a handler OcrOptions ocrOptions = new OcrOptions(handler); // Create an instance of TextOptions to use OCR TextOptions options = new TextOptions(false, true, ocrOptions); // Extract text using OCR using (TextReader reader = parser.GetText(options)) { // Print text or 'not supported' message Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd()); } if (handler.HasWarnings) { Console.WriteLine("The following warnings occur while the text recognition:"); foreach (string w in handler.Warnings) { Console.WriteLine("\\t* " + w); } } else { Console.WriteLine("the text recognition was performed without any warning."); } } ```