IRONSOFTWAREHOME

How to Train a Custom Font with Tesseract 5 in C#

Kannaopat Udonpant
Kannapat Udonpant
Updated: August 2, 2026

The default Tesseract English model misreads plenty of real-world inputs: hospital handwritten intake forms, vintage book digitizations, a game studio's bespoke decorative typeface, or industry-specific symbols a generic OCR engine has never seen. The fix is to train Tesseract on the exact font yourself, producing a single .traineddata artifact you can ship anywhere IronOCR runs.

This guide walks through Tesseract 5 custom font training end to end in C#: install the WSL2 Ubuntu toolchain, render .box and .tif training files from your .ttf or .otf, build the .traineddata model with tesstrain against a base eng.traineddata, then load the result in IronOCR. Once trained, the file is portable across Windows, macOS, Linux, and Docker.

Quickstart: Use Your Trained Font File in C#

Configure IronOCR by pointing UseCustomTesseractLanguageFile at your trained .traineddata file, then call Read on any image as you would with a stock language pack.

  1. 1Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. 2Copy and run this code snippet.

    using IronOcr;
    
    var ocr = new IronTesseract();
    ocr.UseCustomTesseractLanguageFile("path/to/YourCustomFont.traineddata");
    string text = ocr.Read("image-with-special-font.png").Text;
    C#
  3. 3Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer

How Do I Set Up the Training Environment?

How Do I Install IronOCR?

Install IronOCR via NuGet:

PM > Install-Package IronOcr

The DLL package is a manual alternative if you cannot use NuGet. For the underlying engine, see the Tesseract 5 features guide and the custom language reference.

How Do I Install and Set Up WSL2 and Ubuntu?

Refer to the tutorial on Setting up WSL2 and Ubuntu.

Please note: Custom font training requires Linux.

WSL2 is enough: once training is done, the resulting .traineddata file ships with your IronOCR app on Windows, macOS, Linux, or Docker. For deployment details, see the Linux deployment guide.

How Do I Install Tesseract 5 on Ubuntu?

Use these commands to install Tesseract 5:

sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
SHELL

The tesseract-ocr package is the engine that runs recognition; libtesseract-dev exposes the headers that tesstrain needs to build a model. Once your trained file is in use, the Tesseract configuration guide covers runtime tuning.

How Do I Prepare the Font for Training?

Which Font Should I Download?

This tutorial uses the AMGDT font, in either .ttf or .otf format.

Windows File Explorer showing downloaded AMGDT Regular.ttf font file highlighted in red box for training

When picking a font to train:

  • Pick fonts the default English model already misreads. Training a font that's already recognized wastes time.
  • Confirm the font's license permits redistribution if your .traineddata will ship with an application.
  • Decorative, handwritten, and industry-specific fonts (medical, legal, cartographic) gain the most accuracy from training.
  • Match training samples to what production will actually see, including resolution and lighting.

How Do I Mount the Disk Drive?

Mount Drive D: as your working space:

cd /
cd /mnt/d
SHELL

WSL2 mounts every Windows drive under /mnt/<letter>, so you can edit files on Windows and run training commands against them in the same session.

How Do I Copy the Font File to Ubuntu Font Folder?

Tesseract renders sample text in your font to build training images, so the font has to be installed on the Linux side, not just on Windows. Copy the font file to both Ubuntu font directories: /usr/share/fonts and /usr/local/share/fonts. The simplest way is to type \wsl$ in File Explorer's address bar to browse the Ubuntu filesystem from Windows, then drag the .ttf across.

Windows File Explorer showing \wsl$ network path for accessing Ubuntu filesystem from Windows

Here's how the font copy should look once it lands in the Ubuntu fonts directory:

AMGDT font file being copied into the Ubuntu fonts folder and recognized by the system

What If I Get Destination Folder Access Denied?

If File Explorer rejects the copy, run it from a root shell instead:

cd /
su root
cd /c/Users/Admin/Downloads/'AMGDT Regular'
cp 'AMGDT Regular.ttf' /usr/share/fonts
cp 'AMGDT Regular.ttf' /usr/local/share/fonts
exit
SHELL

How Do I Clone the Training Repositories from GitHub?

The training pipeline depends on three repositories. Clone the tutorial wrapper first, then the two upstream Tesseract repos inside it, then create the output folder:

git clone https://github.com/astutejoe/tesseract_tutorial.git
cd tesseract_tutorial
git clone https://github.com/tesseract-ocr/tesstrain
git clone https://github.com/tesseract-ocr/tesseract
mkdir tesstrain/data
SHELL
  • Tesseract_tutorial bundles the Python scripts and config files that drive each training step (text generation, image rendering, training-pair creation).
  • tesstrain contains the Makefile that drives the actual training run.
  • Tesseract contains the tessdata folder with stock .traineddata files used as the starting model for custom training.
  • tesstrain/data is where generated .box files (character bounding boxes), .tif images, and intermediate LSTM checkpoints all land.

Here's how the clone sequence should look in the terminal:

Terminal running the four git clone commands and creating the tesstrain data folder

For working with multiple language packs alongside a custom one, see our international languages guide.

How Do I Generate Training Files?

How Do I Run the split_training_text.py Script?

From the Tesseract_tutorial folder, run:

python split_training_text.py
SHELL

The script generates one .box / .tif pair per training sample and writes them to the data folder.

Here's how the script run should look as it generates the training pairs:

Terminal running split_training_text.py and generating .box and .tif files in the data folder

How Do I Fix Fontconfig Warning?

Terminal showing fontconfig warnings about missing Apex font and empty font directory errors

If you see the warning Fontconfig warning: "/tmp/fonts.conf, line 4: empty font directory name ignored", fontconfig cannot resolve the font directories. Fix it by editing tesseract_tutorial/fonts.conf:

<dir>/usr/share/fonts</dir>
<dir>/usr/local/share/fonts</dir>
<dir prefix="xdg">fonts</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>
XML

Copy it to /etc/fonts:

cp fonts.conf /etc/fonts
SHELL

Then point split_training_text.py at the same path:

fontconf_dir = '/etc/fonts'
Python

How Many Training Files Should I Generate?

By default the script generates 100 training pairs. Change the count near the top of split_training_text.py:

Python code setting count=100 and slicing lines array to limit training data size

Sizing guidance:

  • 100-500 samples are enough to confirm the pipeline works end-to-end.
  • 1000-5000 samples are the working range for production accuracy.
  • Training text must cover every character your font needs to recognize, ideally several times each.
  • More samples mean more training time; pick the smallest count that hits your accuracy target.

Where Do I Download the eng.traineddata File?

Download eng.traineddata from the tessdata_best repository and place it in Tesseract_tutorial/tesseract/tessdata.

The base model gives the trainer linguistic context (which character sequences form plausible words), so accuracy is much better than training from scratch. Pick a base model in the same language as your training text. If you hit issues, see the custom OCR language packs troubleshooting guide.

How Do I Build My Custom Font Trained Data File?

From the tesstrain folder, run:

TESSDATA_PREFIX=../tesseract/tessdata make training MODEL_NAME=AMGDT START_MODEL=eng TESSDATA=../tesseract/tessdata MAX_ITERATIONS=100
SHELL
  • MODEL_NAME is the name of your custom font (used for the output filename).
  • START_MODEL is the base .traineddata you downloaded above.
  • MAX_ITERATIONS caps the training run; higher values typically reduce error rate.

What If I Get "Failed to Read Data" in Makefile?

To resolve "Failed to read data" errors, patch the Makefile:

WORDLIST_FILE := $(OUTPUT_DIR2)/$(MODEL_NAME).lstm-word-dawg
NUMBERS_FILE := $(OUTPUT_DIR2)/$(MODEL_NAME).lstm-number-dawg
PUNC_FILE := $(OUTPUT_DIR2)/$(MODEL_NAME).lstm-punc-dawg
Text

The patch points the Makefile at the actual output directory so it can locate the dictionary files.

How Do I Fix "Failed to Load Script Unicharset"?

Download Latin.unicharset from langdata_lstm and place it in the tesstrain/data/langdata folder.

The .unicharset file defines which characters the trainer is allowed to emit. Use the file that covers every character in your font, for example Cyrillic.unicharset for Cyrillic fonts or Devanagari.unicharset for Devanagari.

Here's how a successful training run should look as tesstrain produces the .traineddata file:

tesstrain build pipeline running through training iterations and emitting the AMGDT.traineddata file

How Do I Verify the Accuracy of My Trained Data File?

With 1000 .box and .tif files and 3000 training iterations, the output AMGDT.traineddata reaches a training error rate (BCER) of around 5.77%.

Tesseract training log showing BCER improvement from 6.388% to 5.771% over iterations 2194-2298

To test the trained model with IronOCR, point UseCustomTesseractLanguageFile at the file and read a sample image:

using IronOcr;

// Load the trained model; AutoOsd handles orientation
var ocr = new IronTesseract();
ocr.UseCustomTesseractLanguageFile("path/to/AMGDT.traineddata");
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;

// Preprocess so the model sees clean glyphs
using var input = new OcrInput();
input.LoadImage("test-image-with-amgdt-font.png");
input.EnhanceResolution(300);
input.DeNoise();

// Confidence reflects training quality
var result = ocr.Read(input);
Console.WriteLine($"Text: {result.Text}");
Console.WriteLine($"Confidence: {result.Confidence}%");

The Confidence property is the per-document score; if it stays low even on clean inputs, the most common causes are too few training samples or a base model that doesn't match the script. Once your .traineddata is verified, see our custom language guide for the general workflow of loading any custom language file.

What Are the Key Takeaways for Custom Font Training?

Training a custom font is a one-time setup: generate .box / .tif pairs from your target font, build a .traineddata model with tesstrain, then load it through UseCustomTesseractLanguageFile. From there IronOCR reads images with the new model exactly the same way it reads stock English.

Key advantages of using IronOCR with a custom Tesseract model:

  • Reuses standard Tesseract artifacts: any .traineddata file you can build with tesstrain works in IronOCR without conversion.
  • Cross-platform output: training requires Linux (or WSL2), but the trained file ships with your application on Windows, macOS, Linux, and Docker.
  • Drop-in with the rest of the API: combine custom fonts with multiple secondary languages, image quality correction, and DPI tuning without changing the recognition path.
  • Tunable accuracy: error rate is a function of training samples times iterations. Both knobs are exposed (the script's sample count plus MAX_ITERATIONS) so you can dial in the trade-off between training time and BCER without leaving Tesseract.

For larger pipelines, consider progress tracking and async processing when applying your trained model across many documents.

Frequently Asked Questions

How can I train a custom font with Tesseract 5 for use with IronOCR?

Training a custom font with Tesseract 5 involves installing WSL2, creating training files from your font, building a custom `.traineddata` model, and loading it into IronOCR using the `UseCustomTesseractLanguageFile` method.

What are the prerequisites for custom font training with Tesseract 5?

You need WSL2 with Ubuntu, Tesseract 5 installed, and IronOCR, which can be downloaded via NuGet for reading with custom-trained fonts.

How do I integrate a custom-trained font into the IronOCR workflow?

Once you have your `.traineddata` file, use IronOCR's `UseCustomTesseractLanguageFile` method to point to the trained font file and then use the `Read` method to process images containing the custom font.

What are the benefits of using a custom Tesseract model with IronOCR?

IronOCR seamlessly integrates any custom `.traineddata` file, allowing cross-platform deployment and combination with other OCR features like multiple language processing and image quality correction.

How do I handle common setup issues when training custom fonts for IronOCR?

Ensure all dependencies like the Tesseract engine and training scripts are correctly installed. Use provided guides for setting up WSL2, checking errors in configuration files, and adjusting training parameters.

Can I deploy my custom-trained Tesseract model on multiple platforms?

Yes, once trained on a Linux environment, the `.traineddata` file is portable and can be used in applications running on Windows, macOS, Linux, and Docker.

What accuracy can I expect from a custom-trained font model in IronOCR?

The accuracy depends on training samples and iterations during the training process. More samples and iterations generally enhance accuracy, and IronOCR provides confidence scores to evaluate the model.

How can I troubleshoot 'Failed to Read Data' errors during training?

This error can often be resolved by patching the Makefile to correctly point it toward the output directory for dictionary files.

What is the role of the `eng.traineddata` file in custom font training?

`eng.traineddata` serves as the base language model providing linguistic context, which improves the accuracy of the custom-trained font over models built entirely from scratch.

What should I do if I encounter permission issues copying font files during setup?

If you receive a 'Destination Folder Access Denied' message, perform the file copy operation from a root shell in the terminal to ensure proper permissions.

Ready to Get Started?

Nuget Downloads 6,236,385Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronOcr
nuget.org/packages/IronOcr/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronOCR"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronOCR to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronOCR.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required