Optical Character Recognition (OCR) Made Easy with the .NET PDF Library in C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (179).NET Core  (29).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (225)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (68)Flutter  (133)JavaScript  (226)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (938)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (162)Xamarin  (161)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (157)Chart  (139)Chart of the week  (53)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (68)Development  (654)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (14)Excel  (43)Extensions  (22)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (14)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (611)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Optical Character Recognition OCR Made Easy with .NET PDF Library in C#

Optical Character Recognition (OCR) Made Easy with the .NET PDF Library in C#

Optical character recognition (OCR) technology plays a vital role in transforming printed or handwritten text into editable and searchable digital content. With the advancements in OCR algorithms, extracting information from PDFs, images, and scanned documents has become more accurate and efficient.

In this blog, we’ll explore how the Syncfusion PDF Library simplifies the process of implementing OCR in your apps, making it easy to extract text and data from scanned documents, images, and PDF documents.

Say goodbye to tedious PDF tasks and hello to effortless document processing with Syncfusion's PDF Library.

What is optical character recognition (OCR)?

Optical Character Recognition (OCR) is a technology used to convert scanned paper documents in the form of PDF files or images into searchable and editable data. This technology uses advanced algorithms to recognize characters, symbols, and patterns within an image and then translates them into machine-encoded text.

The Syncfusion OCR processor library has extended support to process OCR on scanned PDF documents and images with the help of Google’s Tesseract Optical Character Recognition engine.

Tesseract engine

Tesseract is an open-source optical character recognition (OCR) engine. It is one of the most widely used OCR engines in the world and is known for its high accuracy and versatility.

Note: The starting supported version of Tesseract in ASP.NET Core is 4.0.

Why Choose the Syncfusion PDF Library for OCR?

The Syncfusion PDF Library is a robust and feature-rich tool that provides developers with a seamless way to integrate OCR capabilities into their apps. Here are some key reasons why Syncfusion stands out:

  • High accuracy: The Syncfusion PDF Library uses the Tesseract OCR engine, renowned for being one of the most accurate OCR engines available. As a result, the library achieves remarkably high accuracy when extracting text from diverse sources, including challenging inputs like low-resolution scans or intricate images.
  • Multiple language support: The library supports a wide range of languages, making it suitable for apps that require multilingual OCR capabilities. The dictionary packs for the languages can be downloaded from this GitHub repository.
  • Cross-platform compatibility: The Syncfusion OCR processor library works seamlessly in various platforms: Azure App Services, Azure Functions, AWS Textract, Docker, WinForms, WPF, Blazor, ASP.NET MVC, ASP.NET Core with Windows, MacOS, and Linux.

In this blog, we’ll delve into the capabilities of the Syncfusion OCR processor library, enabling users to harness the power of OCR. The article will cover the following topics:

Getting started with app creation

  1. First, we need to create a .NET console app using Visual Studio.
  2. Then, open the Package Manager Console from Tools -> NuGet package Manager -> Package Manager Console.
  3. Execute the following command to install the Syncfusion.PDF.OCR.Net.Core NuGet package.
    Install-Package Syncfusion.PDF.OCR.Net.Core

Unleash the full potential of Syncfusion's PDF Library! Explore our advanced resources and empower your apps with cutting-edge functionalities.

OCR for an entire scanned paper document

Leveraging our library, you can transform a complete scanned PDF document into a searchable PDF. This enables quick and efficient access to the extracted textual content.

Follow these steps to perform OCR on an entire scanned PDF document using our .NET PDF Library:

  1. First, create an instance of the OCRProcessor class to initiate the OCR process.
  2. Load the desired PDF document using the PdfLoadedDocument class.
  3. Specify the OCR language using the OCRSettings class.
  4. Execute OCR on the input document using the PerformOCR method.
  5. Finally, save the processed PDF document into a memory stream using the Save method. 

The following code example shows how to convert an entire scanned PDF into a searchable PDF document.

//Initialize the OCR processor.
using (OCRProcessor processor = new OCRProcessor())
{
    //Load an existing PDF document.
    FileStream inputPDFstream = new FileStream("Input.pdf", FileMode.Open);
    PdfLoadedDocument document = new PdfLoadedDocument(inputPDFstream);
    //Set OCR language.
    processor.Settings.Language = "lat";
    //Perform OCR with input document.
    processor.PerformOCR(document, "Tessdata/");  
    //Create file stream.
    using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        //Save the PDF document to file stream.
        document.Save(outputFileStream);
    }
}

By executing this code example, you will get a PDF document like in the following screenshot.

Performing OCR for an entire scanned paper document
Performing OCR for an entire scanned paper document

OCR for a region of the scanned PDF document

Our .NET PDF Library lets you perform OCR on specific regions or multiple regions of a scanned PDF document.

Follow these steps to perform OCR for a region of the scanned PDF document:

  1. Create the OCRProcessor class to begin the OCR process.
  2. Load the required PDF document using the PdfLoadedDocument class.
  3. Specify the OCR language using the OCRSettings class.
  4. Create a PageRegion object to define the specific region(s) of the document that need OCR.
  5. Set the properties of the page index and page regions using the PageIndex and PageRegions properties, respectively.
  6. Add the defined region(s) to a PDF document’s page regions using the Add method.
  7. Assign the page regions to the Regions property of the OCRSettings class.
  8. Execute OCR on the input document using the PerformOCR method.
  9. Save the processed PDF document to the memory stream using the Save method.

The following code example shows how to convert a region in a scanned PDF into a searchable PDF document.

//Initialize the OCR processor.
using (OCRProcessor processor = new OCRProcessor())
{
    //Load a PDF document.
    FileStream inputPDFStream = new FileStream("Input.pdf", FileMode.Open);
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPDFStream);
    //Set OCR language to process.
    processor.Settings.Language = "lat";
    RectangleF rectangle = new RectangleF(0, 100, 950, 150);
    //Assign rectangles to the page.
    List>PageRegion> pageRegions = new List>PageRegion>();
    PageRegion region = new PageRegion();
    region.PageIndex = 0;
    region.PageRegions = new RectangleF[] { rectangle };
    pageRegions.Add(region);
    processor.Settings.Regions = pageRegions;
    //Process OCR by providing the PDF document.
    processor.PerformOCR(loadedDocument, "Tessdata/");
    //Create file stream.
    using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        //Save the PDF document to file stream.
        loadedDocument.Save(outputFileStream);
    }
}

By executing this code example, you will get a PDF document like in the following screenshot.

Performing OCR for a region of the scanned PDF document
Performing OCR for a region of the scanned PDF document

Embark on a virtual tour of Syncfusion's PDF Library through interactive demos.

OCR on image and converting it to a PDF document

You can transform any scanned image into a searchable and selectable PDF document. Follow these steps to perform OCR on a scanned image and convert it into a searchable PDF:

  1. Instantiate the OCRProcessor class to begin the OCR process.
  2. Load the image from a local file using the FileStream class.
  3. Specify the OCR language using the OCRSettings class.
  4. Execute OCR by providing the bitmap image using the PerformOCR method, and the result will generate a PDF document.
  5. Save the generated PDF document to the memory stream using the Save method.

Following is the code example demonstrating how to convert a scanned image into a searchable and selectable PDF document.

//Initialize the OCR processor.
using (OCRProcessor processor = new OCRProcessor())
{
    //Get stream from an image file. 
    FileStream imageStream = new FileStream(@"Input.jpg", FileMode.Open);
    //Set OCR language to process.
    processor.Settings.Language = Languages.English;
    //Process OCR by providing the bitmap image.  
    PdfDocument document = processor.PerformOCR(imageStream);
    //Create file stream.
    using (FileStream outputFileStream = new FileStream(@"Output.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        //Save the PDF document to file stream.
        document.Save(outputFileStream);
    }
}

By executing this code example, you will get a PDF document like in the following screenshot.

Performing OCR on an image and converting it to a PDF document
Performing OCR on an image and converting it to a PDF document

OCR for a rotated page of a PDF document

Follow these steps to get the text from a rotated page of a PDF document:

  1. Create an instance of the OCRProcessor class to initiate the OCR process.
  2. Load the desired PDF document using the PdfLoadedDocument class.
  3. Specify the OCR language using the OCRSettings class.
  4. Use the PageSegment property and set its value to AutoOsd through the PageSegmentMode enum. This will enable automatic optical character recognition for the entire page.
  5. Execute OCR on the input document using the PerformOCR method.
  6. Save the processed PDF document to a memory stream using the Save method.

Refer to the following code example to perform OCR on an alternated PDF document.

//Initialize the OCR processor.
using (OCRProcessor processor = new OCRProcessor())
{
    //Load an existing PDF document.
    FileStream stream = new FileStream("Input.pdf", FileMode.Open);
    PdfLoadedDocument document = new PdfLoadedDocument(stream);
    //Set OCR language.
    processor.Settings.Language = "lat";
    //Set OCR page auto detection rotation.
    processor.Settings.PageSegment = PageSegMode.AutoOsd;
    //Perform OCR with input document and tessdata (Language packs).
    string extractedText = processor.PerformOCR(document, "Tessdata/");
    //Writes the text to the file.
    File.WriteAllText("OCR.txt", extractedText);
}

By executing this code example, you will get a PDF document like in the following screenshot.

Performing OCR on a rotated page of a PDF document
Performing OCR on a rotated page of a PDF document

See a world of document processing possibilities in Syncfusion's PDF Library as we unveil its features in interactive demonstrations.

Get text and bounds from scanned PDF document

You can also easily obtain text and its corresponding bounds from a scanned PDF document. Follow these steps to achieve this functionality:

  1. Create an instance of the OCRProcessor class to initiate the OCR process.
  2. Load the desired PDF document using the PdfLoadedDocument class.
  3. Specify the OCR language using the OCRSettings class.
  4. Create a layout using the OCRLayoutResult class to capture OCR data and layout information.
  5. Execute OCR on the input document using the PerformOCR method.
  6. Obtain the line collection from the first page using the OCRLineCollection class.
  7. Access each line and its respective bounds using the Line class to retrieve accurate text and positional information.

The following code example explains how to retrieve text and its bounds from a scanned PDF document with OCR.

//Initialize the OCR processor.
using (OCRProcessor processor = new OCRProcessor())
{
    //Load an existing PDF document.
    FileStream stream = new FileStream("Input.pdf", FileMode.Open);
    PdfLoadedDocument document = new PdfLoadedDocument(stream);

    //Set OCR language.
    processor.Settings.Language = "lat";
    //Create the layout result. 
    OCRLayoutResult layoutResult = new OCRLayoutResult();
    //Perform OCR with input document and tessdata (Language packs).
    processor.PerformOCR(document, @"Tessdata/", out layoutResult);
    //Get line collection from first page.
    OCRLineCollection lines = layoutResult.Pages[0].Lines;
    //Get each line and its bounds.
    foreach (Line line in lines)
    {
        string text = line.Text;
        RectangleF bounds = line.Rectangle;
    }

    //Close the document.
    document.Close(true);
}

Perform OCR with Unicode characters

Follow these steps to execute OCR on images containing Unicode characters:

  1. Create an instance of the OCRProcessor class to initiate the OCR process.
  2. Load the desired PDF document using the PdfLoadedDocument class.
  3. Specify the OCR language using the OCRSettings class.
  4. Preserve the Unicode characters in a PDF document using the UnicodeFont property.
  5. Execute OCR on the input document using the PerformOCR method.
  6. Save the processed PDF document to a memory stream using the Save method.

The following code example demonstrates how to perform OCR with Unicode characters in an image file.

//Initialize the OCR processor by providing the path of tesseract.
using (OCRProcessor processor = new OCRProcessor())
{
    //Get stream from an existing PDF document. 
    FileStream stream = new FileStream(Path.GetFullPath(@"UnicodePDF.pdf"), FileMode.Open);
    //Load the PDF document. 
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
    //Sets Unicode font to preserve the Unicode characters in a PDF document.
    FileStream fontStream = new FileStream(Path.GetFullPath(@"ARIALUNI.ttf"), FileMode.Open);
    //Set the font for unicode text. 
    processor.UnicodeFont = new PdfTrueTypeFont(fontStream, 8);
    //Set OCR language to process
    processor.Settings.Language = Languages.English;
    //Perform OCR by providing the PDF document.
    string ocrText = processor.PerformOCR(loadedDocument);
    //Create file stream.
    using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
    {
        //Save the PDF document to file stream.
        loadedDocument.Save(outputFileStream);
    }
}

By executing this code example, you will get a PDF document like in the following screenshot.

Performing OCR with Unicode characters in a PDF
Performing OCR with Unicode characters in a PDF

GitHub reference

You can find the examples for all these OCR options in this GitHub repository.

Join thousands of developers who rely on Syncfusion for their PDF needs. Experience the difference today!

Conclusion

Thanks for reading! In this blog, we’ve seen how easy it is to perform OCR on PDF documents using the Syncfusion .NET PDF Library using C#. With this, you can easily extract text from scanned PDF documents and images. The library also supports a variety of languages, so you can extract text from documents in several languages.

Take a moment to peruse our documentation, where you’ll find other options and features, all with accompanying code examples.

For current Syncfusion customers, the newest version of Essential Studio is available from the license and downloads page. If you are not a customer, try our 30-day free trial to check out these new features.

If you have any questions about these features, please let us know in the comments below. You can also contact us through our support forumsupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed