Perform OCR on macOS

17 Jul 20265 minutes to read

The .NET OCR library is used to extract text from scanned PDFs and images in .NET console applications on macOS with the help of Google’s Tesseract Optical Character Recognition engine.

Prerequisites

Version Compatibility

  • Syncfusion.PDF.OCR.Net.Core supports .NET 8.0 and later versions.

Supported Inputs

The OCR processor supports the following input formats:

  • Single-page and multi-page PDF documents
  • Scanned images in common formats (JPEG, PNG, TIFF)
  • Recommended DPI: 200 DPI or higher for optimal OCR accuracy

Required Software

  • .NET 8 SDK or later
  • Visual Studio or Visual Studio Code

Register the License Key

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you must add the Syncfusion.Licensing assembly reference and register a license key in your application. For more information, see the licensing documentation.

Include the following code in the Program.cs file to register the license key:

using Syncfusion.Licensing;

// Register Syncfusion license at application startup
SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");

NOTE

  1. Beginning from version 21.1.x, the TesseractBinaries and Tesseract language data folders are now included by default; you no longer have to set these paths explicitly.
  2. The current NuGet package includes Tesseract 5.0, which provides support for 100+ languages.

Steps to perform OCR on an entire PDF document on macOS

Step 1: Create a new .NET Core console application project. Mac OS console application

Step 2: Select your target framework (.NET 8.0 or later).

Step 3: Install the Syncfusion.PDF.OCR.Net.Core NuGet package into your project from NuGet.org. Mac OS NuGet path

Step 4: Include the following namespaces in the Program.cs file.

using Syncfusion.OCRProcessor;
using Syncfusion.Pdf.Parsing;

Step 5: Add the following code sample to the Program.cs file to perform OCR on a PDF document.

//Initialize the OCR processor.
using (OCRProcessor processor = new OCRProcessor())
{
   FileStream fileStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
   //Load a PDF document.
   PdfLoadedDocument lDoc = new PdfLoadedDocument(fileStream);
   //Set the Tesseract version
   processor.Settings.TesseractVersion = TesseractVersion.Version5_0;
   //Set the OCR language to be processed.
   processor.Settings.Language = Languages.English;
   //Process OCR by providing the PDF document.
   processor.PerformOCR(lDoc);
   //Save the document to disk.
   lDoc.Save("OCR.pdf");
   lDoc.Close();
   fileStream.Dispose();
   Console.WriteLine("OCR processing completed successfully!");
}

Step 6: Build the project.

Click Build > Build Solution or press Ctrl+Shift+B to build the project.

Step 7: Run the project.

Click the Start button (green arrow) or press F5 to run the app.

Step 1: Open the terminal (Ctrl+`) and run the following command to create a new .NET Core console application project:

dotnet new console -n CreatePdfMacOSApp

Step 2: Replace CreatePdfMacOSApp with your desired project name.

Step 3: Navigate to the project directory using the following command:

cd CreatePdfMacOSApp

Step 4: Use the following command in the terminal to add the Syncfusion.PDF.OCR.Net.Core package to your project:

dotnet add package Syncfusion.PDF.OCR.Net.Core

Step 5: Include the following namespaces in the Program.cs file.

using Syncfusion.OCRProcessor;
using Syncfusion.Pdf.Parsing;

Step 6: Add the following code sample to the Program.cs file to perform OCR on a PDF document.

//Initialize the OCR processor.
using (OCRProcessor processor = new OCRProcessor())
{
   FileStream fileStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
   //Load a PDF document.
   PdfLoadedDocument lDoc = new PdfLoadedDocument(fileStream);
   //Set the Tesseract version.
   processor.Settings.TesseractVersion = TesseractVersion.Version5_0;
   //Set the OCR language to be processed.
   processor.Settings.Language = Languages.English;
   //Process OCR by providing the PDF document.
   processor.PerformOCR(lDoc);
   //Save the document to disk.
   lDoc.Save("OCR.pdf");
   lDoc.Close();
   fileStream.Dispose();
   Console.WriteLine("OCR processing completed successfully!");
}

Step 7: Build the project.

Run the following command in the terminal to build the project:

dotnet build

Step 8: Run the project.

Run the following command in the terminal to run the project:

dotnet run

By executing the program, you will get a PDF document with extracted text as follows.
OCR output document

A complete working sample can be downloaded from GitHub.

Click here to explore the rich set of Syncfusion® PDF library features.