Extract Passport Data in C# with IronOCR
IronOCR's ReadPassport method extracts structured data from passport images including names, passport numbers, birth dates, and expiry dates in a single line of C# code, making it ideal for immigration and security applications.
In applications and systems such as check-in counters and security immigration at airports, where agents handle large volumes of passports daily, having a reliable system that accurately extracts mission-critical traveler information is crucial to ensuring an efficient and streamlined immigration process. The IronOCR library provides advanced OCR capabilities specifically optimized for passport reading, leveraging Tesseract 5 under the hood with machine learning enhancements.
Quickstart: Extract Passport MRZ Info in One LineThis example shows how to load a passport image using OcrInput, use ReadPassport() to extract data, and access structured fields like names, number, and dates from the returned PassportInfo. No complex setup required - just one straightforward line. Unlike traditional Tesseract implementations, IronOCR provides a simplified API specifically designed for document extraction.
-
1Install IronOCR with NuGet Package Manager
-
2Copy and run this code snippet.
var input = new IronOcr.OcrInput(); input.LoadImage("passport.jpg"); var passportInfo = new IronOcr.IronTesseract().ReadPassport(input).PassportInfo;C# -
3Deploy to test on your live environment
Start using IronOCR in your project today with a free trial
Minimal Workflow (5 steps)
- Download a C# library to read passports
- Import the passport image for reading
- Ensure the document contains only the passport image, without headers or footers
- Use the
ReadPassportmethod to extract data from the image - Access the OcrPassportResult property to view and further manipulate the extracted passport data
How Do I Extract Passport Data in C#?
As an example, we will use a passport image as input to showcase the functionality of IronOCR. After loading the image using OcrInput, you can utilize the ReadPassport method to identify and extract information from the passport. This method returns an OcrPassportResult object, which contains properties such as GivenNames, Country, PassportNumber, Surname, DateOfBirth, DateOfExpiry, Gender, DocumentType, Nationality, NationalityCode, IssuingCountryCode, PersonalNumber, and DateOfIssue.
The ReadPassport method is part of IronOCR's specialized document reading capabilities, which also includes methods for reading license plates, MICR cheques, and other structured documents. The method uses advanced computer vision techniques to locate and extract the MRZ (Machine Readable Zone) area automatically.
ReadPassport method currently doesn't automatically rotate the input. When using the input, please ensure the MRZ is always at the bottom of the file; otherwise, the process will fail.What Passport Image Format Should I Use?
IronOCR supports various image formats including JPG, PNG, TIFF, and BMP. For optimal results, ensure your passport image has adequate resolution (at least 300 DPI) and proper lighting. The DPI settings can be adjusted if working with lower quality scans.

What Code Do I Need to Extract Passport Data?
The following example demonstrates the complete process of passport data extraction. For applications processing multiple passports, consider implementing multithreading support to improve performance. You can also track OCR progress for long-running operations.
using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputPassport = new OcrInput();
inputPassport.LoadImage("passport.jpg");
// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);
// Output passport information
Console.WriteLine(result.PassportInfo.GivenNames);
Console.WriteLine(result.PassportInfo.Country);
Console.WriteLine(result.PassportInfo.PassportNumber);
Console.WriteLine(result.PassportInfo.Surname);
Console.WriteLine(result.PassportInfo.DateOfBirth);
Console.WriteLine(result.PassportInfo.DateOfExpiry);
Console.WriteLine(result.PassportInfo.Gender);
Console.WriteLine(result.PassportInfo.DocumentType);
Console.WriteLine(result.PassportInfo.Nationality);
Console.WriteLine(result.PassportInfo.NationalityCode);
Console.WriteLine(result.PassportInfo.IssuingCountryCode);
Console.WriteLine(result.PassportInfo.PersonalNumber);
Console.WriteLine(result.PassportInfo.DateOfIssue);Imports IronOcr
' Instantiate OCR engine
Dim ocr As New IronTesseract()
Using inputPassport As New OcrInput()
inputPassport.LoadImage("passport.jpg")
' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)
' Output passport information
Console.WriteLine(result.PassportInfo.GivenNames)
Console.WriteLine(result.PassportInfo.Country)
Console.WriteLine(result.PassportInfo.PassportNumber)
Console.WriteLine(result.PassportInfo.Surname)
Console.WriteLine(result.PassportInfo.DateOfBirth)
Console.WriteLine(result.PassportInfo.DateOfExpiry)
Console.WriteLine(result.PassportInfo.Gender)
Console.WriteLine(result.PassportInfo.DocumentType)
Console.WriteLine(result.PassportInfo.Nationality)
Console.WriteLine(result.PassportInfo.NationalityCode)
Console.WriteLine(result.PassportInfo.IssuingCountryCode)
Console.WriteLine(result.PassportInfo.PersonalNumber)
Console.WriteLine(result.PassportInfo.DateOfIssue)
End UsingWhat Output Can I Expect from ReadPassport?
The extracted data is returned in a structured format that makes it easy to integrate with existing systems. The OcrResult class provides comprehensive access to all extracted information.

We then access the PassportInfo data member obtained from the OcrPassportResult object. The extraction process automatically handles various passport layouts and formats, providing consistent results across different issuing countries.
GivenNames: A property ofPassportInforeturns the given names of the passport input as a string. This corresponds to the first MRZ data row, positions 4 to 44.Country: A property ofPassportInforeturns the country of the passport input as a string. This corresponds to the first MRZ data row, positions 2 to 3. The returned string spells out the full name of the issuing country instead of the abbreviation. In our example, USA returns 'United States of America'.PassportNumber: A property ofPassportInforeturns the passport number of the passport input as a string. This corresponds to the second MRZ data row, positions 1 to 9.Surname: A property ofPassportInforeturns the passport input's surname as a string. This corresponds to the first MRZ data row, positions 4 to 44.DateOfBirth: A property ofPassportInforeturns the passport input's date of birth as a string in the format YYYY-MM-DD. This corresponds to the second MRZ data row, positions 14 to 19.DateOfExpiry: A property member ofPassportInforeturns the passport input's date of expiry as a string in the format YYYY-MM-DD. This corresponds to the second MRZ data row, positions 22 to 27.Gender: Returns the gender from MRZ position 21 asGender.Male,Gender.Female, orGender.Unspecified.DocumentType: Returns the document type from MRZ positions 1-2, such as "P" for passport, "PS" for service, or "PD" for diplomatic.Nationality: Returns the full English country name derived from MRZ positions 11-13.NationalityCode: Returns the raw ISO 3166-1 alpha-3 code from MRZ positions 11-13.IssuingCountryCode: Returns the raw ISO 3166-1 alpha-3 code for the issuing state from MRZ positions 3-5.PersonalNumber: Returns the optional national ID number from MRZ positions 29-42.DateOfIssue: Returns the date of issue extracted from the Visual Inspection Zone (VIZ) via best-effort label matching across 27 languages.
What MRZ Information Can I Extract from Passports?
IronOCR reads the MRZ information contained at the bottom two rows of any passport that follows the standard of the (International Civil Aviation Organization) ICAO. The MRZ data consists of two data rows, with each set of positions containing unique information. Here's a brief table showing which information corresponds to the index of the row.
The MRZ parsing functionality in IronOCR is designed to handle variations in print quality and image orientation. For challenging documents, you can apply image correction filters to improve recognition accuracy.
What Does the MRZ Section Look Like?
The MRZ is typically located at the bottom of the passport page and consists of two lines of standardized text. Understanding the MRZ structure helps in troubleshooting extraction issues and validating results.

First Row
| Position | Field | Description |
|---|---|---|
| 1 | Document Type | Typically 'P' for passport |
| 2-3 | Issuing Country | Three-letter country code (ISO 3166-1 alpha-3) |
| 4-44 | Surname and Given Names | Surname followed by '<<' and then given names separated by '<' |
Second Row
| Position | Field | Description |
|---|---|---|
| 1-9 | Passport Number | Unique passport number |
| 10 | Check Digit (Passport Number) | Check digit for the passport number |
| 11-13 | Nationality | Three-letter nationality code (ISO 3166-1 alpha-3) |
| 14-19 | Date of Birth | Date of birth in YYMMDD format |
| 20 | Check Digit (Date of Birth) | Check digit for the date of birth |
| 21 | Sex | Gender ('M' for male, 'F' for female, 'X' for unspecified) |
| 22-27 | Date of Expiry | Expiry date in YYMMDD format |
| 28 | Check Digit (Date of Expiry) | Check digit for the date of expiry |
| 29-42 | Personal Number | Optional personal number (usually national ID number) |
| 43 | Check Digit (Personal Number) | Check digit for the personal number |
| 44 | Check Digit (Composite) | Overall check digit |
How Can I Debug and Verify Passport Extraction Results?
We can also verify the results from IronOCR by obtaining the raw extracted text from the passport image and the confidence level to confirm whether the extracted information is accurate. Using the example from above, we can access the Confidence and Text properties of the OcrPassportResult object.
For debugging purposes, you might want to highlight text regions to visually verify what areas were recognized. This feature is particularly useful when troubleshooting extraction issues or optimizing scan regions.
using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputPassport = new OcrInput();
inputPassport.LoadImage("passport.jpg");
// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);
// Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
' Instantiate OCR engine
Private ocr = New IronTesseract()
Private inputPassport = New OcrInput()
inputPassport.LoadImage("passport.jpg")
' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)
' Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence)
Console.WriteLine(result.Text)
Confidence: TheConfidenceproperty fromOcrPassportResultis a float indicating the OCR statistical accuracy confidence as an average of every character. This float is lower if the passport image is blurry or contains other information. One is the highest and most confident, and zero is the lowest and least confident. For production applications, consider implementing result confidence thresholds to ensure data quality.Text: TheTextproperty fromOcrPassportResultcontains the raw, unparsed text extracted from the passport image. Developers can use this in unit tests to verify the extracted text of the passport image. For advanced scenarios, you can export results in hOCR format for further analysis.
Best Practices for Passport Scanning Applications
When implementing passport scanning in production environments, consider these additional factors:
- Image Quality: Ensure input images meet minimum quality standards. The Filter Wizard can help optimize images for better recognition.
- Performance: For high-volume processing, implement async support and consider batch processing multiple passports.
- Security: Since passport data is sensitive, ensure proper data handling and consider integrating with secure document management systems.
- Validation: Implement check digit validation for the extracted MRZ data to ensure accuracy. The MRZ format includes multiple check digits that can be used to verify the integrity of the extracted information.
- Error Handling: Implement robust error handling for cases where passport images may be damaged, poorly lit, or contain non-standard formats.
Frequently Asked Questions
What is IronOCR's ReadPassport method used for?
IronOCR's ReadPassport method is used to extract structured data from passport images, such as names, passport numbers, birth dates, and expiry dates, making it ideal for immigration and security applications.
How can IronOCR help with passport data extraction in C#?
IronOCR simplifies passport data extraction by using the ReadPassport method, which provides a straightforward API for extracting MRZ data, names, passport numbers, and dates in a single line of C# code.
What kind of information can you extract from a passport using IronOCR?
Using IronOCR, you can extract information such as given names, passport number, country, date of birth, nationality code, and other MRZ data fields from a passport.
Is IronOCR specific to any passport language?
Currently, IronOCR's ReadPassport method is optimized for English-based passports but includes advanced computer vision techniques to handle variations in print quality and image orientation.
What are some best practices when using IronOCR for passport scanning?
Best practices include ensuring high-quality images (300 DPI), validating extracted MRZ data with check digits, implementing robust error handling, and using secure data management for sensitive passport data.
What are the requirements for using the ReadPassport method on a Mac?
When using ReadPassport on a Mac, ensure the MRZ is at the bottom of the input image, as automatic rotation is not supported, and the project needs to run in x64 architecture on advanced .NET Framework.
What image formats are supported by IronOCR for passport OCR?
IronOCR supports various image formats, including JPG, PNG, TIFF, and BMP, for passport OCR and recommends images with a resolution of at least 300 DPI.
Can IronOCR handle non-standard passport formats?
IronOCR's MRZ parsing is designed to handle variations in passport print quality and image orientation, making it adaptable to different passport layouts and formats.
How does IronOCR ensure the accuracy of extracted passport data?
IronOCR provides the Confidence property in the OcrPassportResult, which indicates the statistical accuracy of the OCR process, and developers can implement thresholds for quality assurance.
What additional features does IronOCR offer for MRZ extraction validation?
IronOCR includes features like check digit validation for MRZ data fields, ensuring the accuracy and integrity of the extracted information.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.