How to Perform OCR on iOS in .NET MAUI

iOS

.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.

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">
        <PackageReference Include="IronOcr.iOS" Version="YOUR_PACKAGE_VERSION" />
    </ItemGroup>
    <ItemGroup Condition="$(TargetFramework.Contains('ios')) == true">
        <PackageReference Include="IronOcr.iOS" Version="YOUR_PACKAGE_VERSION" />
    </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 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 should remain deactivated.

using System;
using IronOcr;
using Microsoft.Maui.Controls;

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 System;
using IronOcr;
using Microsoft.Maui.Controls;

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 System
Imports IronOcr
Imports Microsoft.Maui.Controls

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
$vbLabelText   $csharpLabel

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"

Frequently Asked Questions

How do I integrate OCR functionality into a .NET MAUI app for iOS?

You can integrate OCR functionality into a .NET MAUI app for iOS by using the IronOCR.iOS package. Install it via NuGet in Visual Studio, and then modify your project file to include the package conditionally for the iOS platform. Use IronTesseract to process images and extract text.

Can I use the IronOCR.iOS package for PDF document processing?

No, the IronOCR.iOS package is currently limited to processing image files only and does not support PDF documents. Ensure any PDF-related configurations are disabled in your project.

What steps are involved in setting up OCR for an iOS app using .NET MAUI?

Setting up OCR for an iOS app using .NET MAUI involves downloading the IronOcr.iOS package via NuGet, modifying your project file to conditionally include the package for iOS, and editing your MainPage.xaml and MainPage.xaml.cs files to create the user interface and handle OCR processing.

What additional requirements are there for using IronOCR in Avalonia projects?

When using IronOCR in Avalonia projects, you need to ensure that you have the latest .NET SDK version and also .NET SDK 8.0.101 installed. This setup is similar to MAUI but requires this additional SDK.

How can I perform OCR on an image using IronOCR in a .NET MAUI project?

In a .NET MAUI project, use the IronTesseract object to perform OCR on an image. Use FilePicker.PickAsync to select an image file, load it into an OcrInput object, and then use IronTesseract to read the image and extract text.

Is there a sample project for implementing OCR on iOS using .NET MAUI?

Yes, you can download a sample .NET MAUI project using IronOCR.iOS from the Iron Software website. This sample project is available as a zipped file that you can open in Visual Studio to expedite your development process.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.