Reading text from images
This guide demonstrates how to extract text from an image using OCR.
Use this workflow when you need to:
- Convert image-based text into editable text output
- Index scanned content for search
- Feed OCR output into validation or automation pipelines
Project setup
Install:
- The core Nutrient Native SDK package
GdPicture.Resourcesfor OCR language resources
Prepare the project
Register the SDK license before running OCR operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager licence = new LicenseManager();licence.RegisterKEY(""); // Set your license keyLoad and prepare the image
Create imaging and OCR instances, load the source image, and set it for OCR:
using GdPictureImaging imaging = new GdPictureImaging();using GdPictureOCR ocr = new GdPictureOCR();
// Load the image containing text to be extractedint imageId = imaging.CreateGdPictureImageFromFile(@"input.png");
// Apply the image to the OCR engine for processingocr.SetImage(imageId);Run text recognition
Execute OCR and read the recognized text result:
// Run OCR analysis to identify and extract textstring ocrResultId = ocr.RunOCR(OCRSpecialContext.None);
// Retrieve the recognized text contentstring result = ocr.GetOCRResultText(ocrResultId);Save extracted text
Write OCR output to a text file:
// Save the extracted text to a fileFile.WriteAllText(@"output.txt", result);Restrict the recognized characters
GdPictureOCR has three properties that control what the engine recognizes. Set them before calling RunOCR:
CharacterSet— The allowlist. The engine returns only the listed characters — for example,"0123456789"for digits only. The default, an empty string, recognizes all characters.CharacterBlackList— The denylist. The engine ignores the listed characters. The default is an empty string.OCRMode—OCRMode.FavorAccuracy(default) orOCRMode.FavorSpeed.
The example below recognizes English text, ignores digits, and checks the status after each step:
using GdPictureImaging filterImaging = new GdPictureImaging();using GdPictureOCR filterOcr = new GdPictureOCR();
int filterImageId = filterImaging.CreateGdPictureImageFromFile(@"input.png");if (filterImaging.GetStat() != GdPictureStatus.OK){ throw new Exception($"Loading the image failed: {filterImaging.GetStat()}");}
try{ filterOcr.SetImage(filterImageId); filterOcr.AddLanguage(OCRLanguage.English); // Ignore digits during recognition. filterOcr.CharacterBlackList = "0123456789";
string filteredResultId = filterOcr.RunOCR(); if (filterOcr.GetStat() != GdPictureStatus.OK) { throw new Exception($"OCR failed: {filterOcr.GetStat()}"); }
File.WriteAllText(@"output_filtered.txt", filterOcr.GetOCRResultText(filteredResultId));}finally{ // Release the OCR results and the image, even if a step fails. filterOcr.ReleaseOCRResults(); filterImaging.ReleaseGdPictureImage(filterImageId);}Imports System.IO
Using filterImaging As New GdPictureImaging() Using filterOcr As New GdPictureOCR() Dim filterImageId As Integer = filterImaging.CreateGdPictureImageFromFile("input.png") If filterImaging.GetStat() <> GdPictureStatus.OK Then Throw New Exception($"Loading the image failed: {filterImaging.GetStat()}") End If
Try filterOcr.SetImage(filterImageId) filterOcr.AddLanguage(OCRLanguage.English) ' Ignore digits during recognition. filterOcr.CharacterBlackList = "0123456789"
Dim filteredResultId As String = filterOcr.RunOCR() If filterOcr.GetStat() <> GdPictureStatus.OK Then Throw New Exception($"OCR failed: {filterOcr.GetStat()}") End If
File.WriteAllText("output_filtered.txt", filterOcr.GetOCRResultText(filteredResultId)) Finally ' Release the OCR results and the image, even if a step fails. filterOcr.ReleaseOCRResults() filterImaging.ReleaseGdPictureImage(filterImageId) End Try End UsingEnd UsingTo make the output searchable inside a PDF instead of saving plain text, refer to the image to searchable PDF guide.
Additional OCR options
Extend this workflow with:
- Language-specific OCR configuration for targeted recognition
- Confidence-based review logic
- Region-based OCR for specific image areas
- Layout-aware post-processing for structured documents
Conclusion
This workflow extracts text from an image and saves it as plain text for downstream processing.