Recently, as a senior Azure developer, I got a requirement on how to extract text from a PDF image. We will start by discussing a very important example: how to extract bill details from a PDF image using Azure Cognitive Services.
Table of Contents
How to extract text from a PDF image
In real-time scenarios, we will encounter many situations where, instead of manually reading the bill, we will have to read the bill details from an image, and we may need to auto-populate these details in a form. Nowadays, we do not do anything manually in every case. We want to implement automation everywhere. We want to automate every process nowadays.
Microsoft helps us provide the Azure Cognitive Service OCR to deal with this scenario. OCR stands for Optical Character Recognition.
Read Text From Images
As we know, using the Azure Cognitive Service, a developer can easily implement an AI feature without any expertise in AI and ML. The developer’s dream comes true. A developer can easily implement the AI features by just calling the Azure Cognitive Service API. Very little effort is involved, and developers can save lots of time to achieve such excellent functionality, i.e., AI.
As part of this functionality development, we will try to extract the date and bill details from the image.
So, before starting the actual development, we should know the Prerequisites needed for this development activity.
Prerequisites
- You must have a valid Azure Subscription or Account. If you don’t have one yet, create an Azure Free Account now.
- Visual Studio 2019 needs to be installed on your Machine. If you don’t have it on your local machine or dev machine, Install Visual Studio 2019 now.
Create Azure Cognitive Service using Azure Portal
To extract text from a PDF image, follow the below steps
1. Log in to the Azure Portal (https://portal.azure.com/)
2. Once you log in to the Azure Portal, you need to search for Cognitive Services. Then click on the search result as shown below

One more way to get the same options: From the left navigation, you can also click on the + Create a resource and search for Cognitive Services. Then click on the search result as shown below

3. The next step is, to click on the Create button as highlighted in the below window.

4. Provide the below details on the Create Cognitive Services window
- Subscription: You must choose the valid subscription you want to use here.
- Resource Group: For this option, you need to choose your existing Resource Group if you have one. If you don’t have an existing Resource Group, you can create a new one by clicking the Create New link.
- Region: Provide the Region for your Azure Cognitive Services.
- Name: Here, provide a unique name for your Azure Cognitive Services.
- Pricing tier: Select the pricing tier that best suits your business needs. Then, click the View full pricing details link to verify the pricing details for your reference.
- Select the two checkboxes below to accept the terms and conditions.
Finally, click on the Review + Create button as highlighted below.

Now, it will check whether all the information you entered is correct. Once it finds that all the details are correct, it will show you Validation Passed. Now, you can see the Create button is enabled. Click on the Create button, as highlighted below, to create the Cognitive Service.

Now, you can see your deployment is completed successfully without any issues. Click on the Go to Resource button to navigate to the Azure Cognitive service you have created.

You can see the new Cognitive service once you click the Go to Resource button.
On the Cognitive service page, click on the Keys and Endpoint option from the left navigation. Now, you can see the Key1 and ENDPOINT values; keep both values and keep them with you, as we will use those values in our code in the following steps. Click on the highlighted copy button to copy those values.

Creating Console App (.NET Core) Visual Studio 2019
The next step is creating the console App (.NET Core) using Visual Studio 2019. Assuming that you have already installed Visual Studio 2019. Let’s start creating the console App (.NET Core).
Open Visual Studio 2019 and click on the Create a New Project button
Select the Console App (.NET Core) as the project template and click Next.

On the Configure your new project window, provide a name for the Project, click Provide the location where you want to save your project, and click the Create button.

The Console App (.NET Core) project will be created successfully without any issues. Once the project is created successfully, the next step is adding a NuGet package.
Right-click on the Project name —–> Click on the Manage NuGet Packages option as shown below

Now, search for Microsoft.Azure.CognitiveServices.Vision.ComputerVision Nuget package, and then you need to select the search result and then click on the Install button, and then click on the I accept button to install the Nuget Package as shown below

Now, if you check the project name.csproj file, it should look like below
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.CognitiveServices.Vision.ComputerVision" Version="5.0.0" />
</ItemGroup>
</Project>
Here is my bill that I have saved in my local machine in C:\Users\Bijay\Desktop\VM\Bill\mybill2.jpg path.

Now, we need to make the code changes for the Program.cs file. So, add the code to your Program.cs file as below.
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using System;
using System.IO;
namespace mydemoapp
{
class Program
{
// put your Cognitive Service Key1 value
static string Key = "12c9e662d55547779ce9785985932118";
// put your Cognitive Service URL
static string url = "https://mynewcognitiveservices91.cognitiveservices.azure.com/";
static void Main(string[] args)
{
var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Key));
client.Endpoint = url;
var myfile = File.OpenRead(@"C:\Users\Bijay\Desktop\VM\Bill\mybill2.jpg");
var result = client.RecognizePrintedTextInStreamAsync(false, myfile);
result.Wait();
var rst = result.Result;
foreach (var r in rst.Regions)
{
foreach (var t in r.Lines)
{
foreach (var w in t.Words)
{
var coordinates = w.BoundingBox.Split(',');
Console.WriteLine($"Found word {w.Text} at" +
$" X {coordinates[0]} and Y {coordinates[1]}");
}
}
}
}
}
}
You can see it here

After you have added the code to the program.cs file, replace the key and URL value with the key and endpoint URL value of your Azure Cognitive Service that you created in the Azure Portal and already copied and kept with you in the above steps.
Now, let’s run the project and see what’s happening. Press F5 to run the project and see if we get the expected output, as shown below.

Conclusion
Azure Cognitive Services offers powerful and flexible solutions for extracting text from PDF images with minimal effort using the above steps mentioned in this article.

I am Rajkishore, and I am a Microsoft Certified IT Consultant. I have over 14 years of experience in Microsoft Azure and AWS, with good experience in Azure Functions, Storage, Virtual Machines, Logic Apps, PowerShell Commands, CLI Commands, Machine Learning, AI, Azure Cognitive Services, DevOps, etc. Not only that, I do have good real-time experience in designing and developing cloud-native data integrations on Azure or AWS, etc. I hope you will learn from these practical Azure tutorials. Read more.
