Getting Started with WPF OCR Processor

14 Aug 20263 minutes to read

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

Prerequisites

Version Compatibility

  • Syncfusion.Pdf.OCR.WPF supports WPF applications targeting .NET Framework 4.6.2 and later, as well as .NET 8.0 for Windows 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

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. Please refer to this link for details on registering a Syncfusion® license key.

To register the license key, add the following code to your App.xaml.cs file at the beginning of the App constructor:

using Syncfusion.Licensing;

public partial class App : Application
{
    public App()
    {
        SyncfusionLicenseProvider.RegisterLicense("YOUR_LICENSE_KEY");
    }
}

Steps to perform OCR on an entire PDF document in WPF

Step 1: Create a new WPF application project.
WPF application creation

In the project configuration window, select your target framework (.NET Framework 4.6.2 or later), name your project, and select Create.
WPF project configuration window

Step 2: Install the Syncfusion.Pdf.OCR.Wpf NuGet package into your WPF application from nuget.org.
OCR NuGet package installation

Step 3: Add a new button in MainWindow.xaml to perform OCR as follows.

<Grid>
    <Button Content="Perform OCR" HorizontalAlignment="Left" Margin="279,178,0,0" VerticalAlignment="Top" Height="68" Width="203" Click="Button_Click"/>
</Grid>

Step 4: Build the project to ensure the XAML compiles and generates the code-behind properly. Press Ctrl+Shift+B or go to Build > Build Solution.

Step 5: Include the following namespaces in the MainWindow.xaml.cs file.

using System.Windows;
using Syncfusion.OCRProcessor;
using Syncfusion.Pdf.Parsing;

Step 6: Add the following code to the Button_Click event handler to perform OCR on the entire PDF document using the PerformOCR method of the OCRProcessor class.

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Initialize the OCR processor.
    using (OCRProcessor processor = new OCRProcessor())
    {
        //Load an existing PDF document.
        PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
        //Set the Tesseract version
        processor.Settings.TesseractVersion = TesseractVersion.Version5_0;
        //Set OCR language to process.
        processor.Settings.Language = Languages.English;
        //Process OCR by providing the PDF document.
        processor.PerformOCR(loadedDocument);  
        //Save the OCR processed PDF document to disk.
        loadedDocument.Save("OCR.pdf");
        loadedDocument.Close(true);
    }
}

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

A complete working sample can be downloaded from GitHub.

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