ForeverTools.OCR 1.0.0

dotnet add package ForeverTools.OCR --version 1.0.0
                    
NuGet\Install-Package ForeverTools.OCR -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ForeverTools.OCR" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ForeverTools.OCR" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ForeverTools.OCR" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ForeverTools.OCR --version 1.0.0
                    
#r "nuget: ForeverTools.OCR, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ForeverTools.OCR@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ForeverTools.OCR&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ForeverTools.OCR&version=1.0.0
                    
Install as a Cake Tool

ForeverTools.OCR

AI-powered OCR for .NET using GPT-4 Vision, Claude 3, and Gemini. Extract text from images, documents, screenshots, receipts, and scanned files with state-of-the-art accuracy.

Features

  • Simple Text Extraction - Extract all text from any image
  • Multiple Input Formats - File path, bytes, base64, URL, or stream
  • Structured Output - Get text with paragraphs, lines, and blocks
  • Table Extraction - Extract tables as structured data or CSV
  • Form Recognition - Extract form fields and values
  • Receipt/Invoice OCR - Parse receipts with merchant, items, totals
  • Multiple Models - GPT-4o, Claude 3, Gemini, and more
  • Async/Await - Fully asynchronous API
  • Dependency Injection - Built-in ASP.NET Core support
  • Multi-Target - .NET 8, .NET 6, .NET Standard 2.0

Installation

dotnet add package ForeverTools.OCR

Quick Start

Get your API key at aimlapi.com.

using ForeverTools.OCR;

var client = new OcrClient("your-api-key");

// Extract text from an image file
var result = await client.ExtractTextFromFileAsync("document.png");
if (result.Success)
{
    Console.WriteLine(result.Text);
}

// Extract from URL
var urlResult = await client.ExtractTextFromUrlAsync("https://example.com/image.jpg");

// Extract from bytes
byte[] imageData = File.ReadAllBytes("photo.jpg");
var bytesResult = await client.ExtractTextAsync(imageData);

Model Selection

Choose the best model for your use case:

// General purpose (default) - best balance
var result = await client.ExtractTextFromFileAsync("doc.png", OcrModels.Gpt4o);

// Fast and cheap - for clear printed text
var fast = await client.ExtractTextFromFileAsync("screenshot.png", OcrModels.Gpt4oMini);

// Handwriting recognition - highest accuracy
var handwriting = await client.ExtractTextFromFileAsync("notes.jpg", OcrModels.Claude3Opus);

// Non-English text - best multilingual support
var foreign = await client.ExtractTextFromFileAsync("document.png", OcrModels.Gemini15Pro);

// Use recommendations helper
var receipt = await client.ExtractTextFromFileAsync("receipt.jpg", OcrModels.Recommendations.Receipts);

Structured Extraction

Extract with Layout

var result = await client.ExtractStructuredAsync(imageBytes);

Console.WriteLine("Paragraphs:");
foreach (var paragraph in result.Paragraphs)
{
    Console.WriteLine($"  - {paragraph}");
}

Console.WriteLine("\nText Blocks:");
foreach (var block in result.Blocks)
{
    Console.WriteLine($"  [{block.BlockType}] {block.Text}");
}

Extract Tables

var result = await client.ExtractTablesAsync(imageBytes);

foreach (var table in result.Tables)
{
    Console.WriteLine($"Table: {table.ColumnCount} columns, {table.RowCount} rows");
    Console.WriteLine(table.ToCsv());
}

Extract Form Fields

var result = await client.ExtractFormFieldsAsync(imageBytes);

foreach (var field in result.Fields)
{
    Console.WriteLine($"{field.Key}: {field.Value}");
}

// Get specific field
var name = result.GetField("Name");
var date = result.GetField("Date");

Extract Receipts

var result = await client.ExtractReceiptAsync(imageBytes);

Console.WriteLine($"Merchant: {result.MerchantName}");
Console.WriteLine($"Date: {result.Date}");
Console.WriteLine($"Total: {result.Total}");
Console.WriteLine($"Tax: {result.Tax}");

Console.WriteLine("\nItems:");
foreach (var item in result.Items)
{
    Console.WriteLine($"  {item.Description} x{item.Quantity} = {item.TotalPrice}");
}

Custom Extraction

Use custom prompts for specialized extraction:

var result = await client.ExtractWithPromptAsync(
    imageBytes,
    "Extract only the email addresses and phone numbers from this business card. Return as JSON with 'emails' and 'phones' arrays."
);

Dependency Injection

// Program.cs
builder.Services.AddForeverToolsOcr("your-api-key");

// Or with options
builder.Services.AddForeverToolsOcr(options =>
{
    options.ApiKey = "your-api-key";
    options.DefaultModel = OcrModels.Gpt4o;
    options.TimeoutSeconds = 90;
    options.MaxTokens = 4096;
    options.ImageDetail = "high"; // "low", "high", or "auto"
});

// Or from configuration
builder.Services.AddForeverToolsOcr(builder.Configuration);

appsettings.json:

{
  "OCR": {
    "ApiKey": "your-api-key",
    "DefaultModel": "gpt-4o",
    "TimeoutSeconds": 60,
    "MaxTokens": 4096,
    "ImageDetail": "auto"
  }
}

Use in your services:

public class DocumentService
{
    private readonly OcrClient _ocr;

    public DocumentService(OcrClient ocr)
    {
        _ocr = ocr;
    }

    public async Task<string> ProcessDocument(byte[] imageData)
    {
        var result = await _ocr.ExtractTextAsync(imageData);
        return result.Success ? result.Text : throw new Exception(result.Error);
    }
}

Environment Variables

// Uses AIML_API_KEY by default
var client = OcrClient.FromEnvironment();

// Or specify custom variable
var client = OcrClient.FromEnvironment("MY_OCR_KEY");

Supported Image Formats

  • PNG, JPEG, GIF, WebP, BMP, TIFF
  • Automatic format detection from bytes
  • URLs to images (publicly accessible)

Error Handling

var result = await client.ExtractTextFromFileAsync("document.png");

if (result.Success)
{
    Console.WriteLine($"Text: {result.Text}");
    Console.WriteLine($"Model: {result.Model}");
    Console.WriteLine($"Tokens: {result.TokensUsed}");
    Console.WriteLine($"Time: {result.ProcessingTimeMs}ms");
}
else
{
    Console.WriteLine($"Error: {result.Error}");
}

Available Models

Model Best For Speed Accuracy
gpt-4o General purpose Fast High
gpt-4o-mini Screenshots, clear text Fastest Good
gpt-4-turbo Scanned documents Medium High
claude-3-5-sonnet Forms, structured docs Fast High
claude-3-opus Handwriting, critical docs Slow Highest
gemini-1.5-pro Non-English, multilingual Medium High
gemini-1.5-flash Quick tasks Fast Good

Other ForeverTools Packages

Package Description NuGet
ForeverTools.AIML Access 400+ AI models (GPT-4, Claude, Llama, DALL-E) NuGet
ForeverTools.APILayer IP geolocation, currency exchange, phone & email validation NuGet
ForeverTools.Captcha Multi-provider captcha solving (2Captcha, CapSolver) NuGet
ForeverTools.Postmark Transactional email sending with templates NuGet
ForeverTools.ScraperAPI Web scraping with proxy rotation NuGet

License

MIT License - see LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 417 12/11/2025