How to Perform OCR on iOS in .NET MAUI

by Chaknith Bin

.NET MAUI (Multi-platform App UI) is an evolution of the Xamarin.Forms framework, designed to create cross-platform apps for Android, iOS, macOS, and Windows using .NET. MAUI aims to simplify the process of building native user interfaces that can run on multiple platforms.

The IronOcr.iOS package brings OCR support to iOS!!

IronOCR iOS Package

The IronOcr.iOS package enables OCR features on iOS devices via .NET cross-platform projects. The vanilla IronOCR package is not needed.

PM > Install-Package IronOcr.iOS
C# NuGet Library for PDF

Install with NuGet

Install-Package IronOcr.iOS

Create a .NET MAUI project

Under the Multiplatform section, select .NET MAUI App and continue.

Create .NET MAUI App project

Include the IronOCR.iOS library

The library can be added in various ways. The easiest is perhaps by using NuGet.

  1. Inside Visual Studio, right-click on "Dependencies > Nuget" and select "Manage NuGet Packages ...".
  2. Select the "Browse" tab and search for "IronOcr.iOS".
  3. Select the "IronOcr.iOS" package and click on "Add Package".

Download IronOcr.iOS package

To prevent issues with other platforms, modify the csproj file to only include the package when targeting the iOS platform. In order to do so:

  1. Right-click on the *.csproj file for your project and select "Edit Project File".
  2. Create a new ItemGroup element as such:
    <ItemGroup Condition="$(TargetFramework.Contains('ios')) == true">
    </ItemGroup>
    XML
  3. Move the "IronOcr.iOS" PackageReference inside the ItemGroup we just created.

The above steps will prevent the "IronOcr.iOS" package from being used on e.g. Android platforms (for that purpose, install IronOcr.Android instead).

Edit "MainPage.xaml"

Edit the XAML file to display a button and a label to show the OCR result. For example:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MAUIIronOCRiOSSample.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button
            Text="Import File"
            Clicked="ReadFileOnImport"
            Grid.Row="0"
            HorizontalOptions="Center"
            Margin="20, 20, 20, 10"/>

        <ScrollView
            Grid.Row="1"
            BackgroundColor="LightGray"
            Padding="10"
            Margin="10, 10, 10, 30">
            <Label x:Name="OutputText"/>
        </ScrollView>
    </Grid>

</ContentPage>
XML

Edit "MainPage.xaml.cs"

Start by instantiating the IronTesseract object. Make sure to initialize IronTesseract once in a class, as shown in the code below. Instantiating it directly in a method is not effective and could lead to unexpected errors. Then, use the FilePicker.PickAsync method to select a file. From the FileResult, open a stream for reading. Create a new OcrInput object and use this object to load the image. Use the tesseract instance to perform OCR on the image and return the text. Finally, display the resulting text in a label.

The current implementation is limited to image files only. The package does not work with PDF documents yet. With this in mind, any configuration related to PDF documents has been by default and should remain deactivated.

using IronOcr;

namespace MAUIIronOCRiOSSample;

public partial class MainPage : ContentPage
{
    // Initialize IronTesseract once in a class
    private IronTesseract ocrTesseract = new IronTesseract();

    public MainPage()
    {
        InitializeComponent();
        // Apply License key
        IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
    }

    private async void ReadFileOnImport(object sender, EventArgs e)
    {
        try
        {
            var options = new PickOptions
            {
                PickerTitle = "Please select a file"
            };

            var result = await FilePicker.PickAsync(options);
            if (result != null)
            {
                using var stream = await result.OpenReadAsync();

                // Instantiate OcrInput
                using var ocrInput = new OcrInput();

                // Load image stream
                ocrInput.LoadImage(stream);

                // Perform OCR
                var ocrResult = ocrTesseract.Read(ocrInput);
                OutputText.Text = ocrResult.Text;
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions
            System.Diagnostics.Debug.WriteLine(ex);
        }
    }
}
using IronOcr;

namespace MAUIIronOCRiOSSample;

public partial class MainPage : ContentPage
{
    // Initialize IronTesseract once in a class
    private IronTesseract ocrTesseract = new IronTesseract();

    public MainPage()
    {
        InitializeComponent();
        // Apply License key
        IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
    }

    private async void ReadFileOnImport(object sender, EventArgs e)
    {
        try
        {
            var options = new PickOptions
            {
                PickerTitle = "Please select a file"
            };

            var result = await FilePicker.PickAsync(options);
            if (result != null)
            {
                using var stream = await result.OpenReadAsync();

                // Instantiate OcrInput
                using var ocrInput = new OcrInput();

                // Load image stream
                ocrInput.LoadImage(stream);

                // Perform OCR
                var ocrResult = ocrTesseract.Read(ocrInput);
                OutputText.Text = ocrResult.Text;
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions
            System.Diagnostics.Debug.WriteLine(ex);
        }
    }
}
Imports IronOcr

Namespace MAUIIronOCRiOSSample

	Partial Public Class MainPage
		Inherits ContentPage

		' Initialize IronTesseract once in a class
		Private ocrTesseract As New IronTesseract()

		Public Sub New()
			InitializeComponent()
			' Apply License key
			IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01"
		End Sub

		Private Async Sub ReadFileOnImport(ByVal sender As Object, ByVal e As EventArgs)
			Try
				Dim options = New PickOptions With {.PickerTitle = "Please select a file"}

				Dim result = Await FilePicker.PickAsync(options)
				If result IsNot Nothing Then
					Dim stream = Await result.OpenReadAsync()

					' Instantiate OcrInput
					Dim ocrInput As New OcrInput()

					' Load image stream
					ocrInput.LoadImage(stream)

					' Perform OCR
					Dim ocrResult = ocrTesseract.Read(ocrInput)
					OutputText.Text = ocrResult.Text
				End If
			Catch ex As Exception
				' Handle exceptions
				System.Diagnostics.Debug.WriteLine(ex)
			End Try
		End Sub
	End Class
End Namespace
VB   C#

Lastly, switch the build target to iOS Simulator and run the project.

Run the Project

This will show you how to run the project and perform OCR.

Execute .NET MAUI App project

Download .NET MAUI App Project

You can download the complete code for this guide. It comes as a zipped file that you can open in Visual Studio as a .NET MAUI App project.

Click here to download the project.

Using IronOcr.iOS in Avalonia

Setting up IronOcr.iOS in Avalonia is similar to MAUI, with one crucial difference: in addition to the latest .NET SDK version, you also need .NET SDK 8.0.101 installed to run IronOcr.iOS successfully. Afterward, IronOcr.iOS can be used in an Avalonia project with the same setup as described above.

If you want to perform OCR on Android, navigate to the following article to learn more: "How to Perform OCR on Android in .NET MAUI"