FineReader Engine OCR & Scripting Languages

Language:
EN
Product-Line:
FineReader Engine
Version:
10, 11
Platform:
Windows
KB-Type:
Code Samples Collection
Category:
Integration, Languages & OCR
Image:
image: icon_script.gif

If your application is written in a scripting languages, for example VBScript, JavaScript, Perl etc. then it is possible to connect it to ABBYYs OCR engine

General Steps

The procedure of recognizing and exporting an image file uses the basic objects and methods of ABBYY FineReader Engine. Do the following:

  • Create the OutprocLoader object for loading the Engine.
  • Create the Engine object using GetEngineObject method provided by the OutprocLoader object.
  • [optional] Load a predefined profile that best fits your needs using the LoadPredefinedProfile method of the Engine object.
  • [optional, PDF only] Check for text layer in the image file using the IsPdfWithTextualContent method of the Engine object. If text layer exists, you can copy the file without recognition, speeding up the processing. However, note that the text layer can be irrelevant.
  • Call the CreateFRDocument method of the Engine object to create an emptyFRDocument object.
  • Call the AddImageFile method of the FRDocument object to add an image from disk to the document.
  • [optional] Create the DocumentProcessingParams object and set the parameters of analysis, recognition, and synthesis.
  • Call the Process method of the FRDocument object.
  • [optional] Create an export parameter object of the type you need. The type of this object depends on the format of the output file you would like to use (FileExportFormatEnum). The default object values are the ones used most frequently, but you may set the export parameters as you like.
  • Call the Export method of the FRDocument object to save the document into a file of the specified format.
  • Call the Close method of the FRDocument object to free the resources used by document.
  • Unload the Engine object using the ExplicitlyUnload method provided by the OutprocLoader object.

Note: This sample uses the OutprocLoader object for loading the Engine. However, you can easily replace it with the InprocLoader object, which works just as well with the scripting languages.

VBScript

' © 2013 ABBYY Production LLC
' SAMPLES code is property of ABBYY, exclusive rights are reserved. 
'
' DEVELOPER is allowed to incorporate SAMPLES into his own APPLICATION and modify it under 
' the  terms of  License Agreement between  ABBYY and DEVELOPER.

 
' ABBYY FineReader Engine 11 Sample

' This sample shows basic steps of ABBYY FineReader Engine usage:
' Initializing, opening image file, recognition and export.

Dim EngineLoader, Engine, FSO
Main()
 
Sub Main
	On Error Resume Next
		' Include config-file SamplesConfig.vbs
		Set FSO = CreateObject( "Scripting.FileSystemObject" )
		Set SamplesConfigFile = FSO.OpenTextFile( "..\SamplesConfig.vbs" )
		ExecuteGlobal SamplesConfigFile.ReadAll()
 
		Run()
	If Err.Number <> 0 Then
		DisplayMessage( Err.Description )
		Err.Clear()
	End If
End Sub
 
Sub Run
	' Load ABBYY FineReader Engine
	LoadEngine()
 
	' Process with ABBYY FineReader Engine
	ProcessWithEngine()
 
	' Unload ABBYY FineReader Engine
	UnloadEngine()
End Sub
 
Sub LoadEngine
	DisplayMessage( "Initializing Engine..." )
	Set EngineLoader = CreateObject( "FREngine.OutprocLoader" )
	Set Engine = EngineLoader.GetEngineObject( GetDeveloperSN() )
End Sub
 
Sub ProcessWithEngine
	On Error Resume Next
		' Setup FREngine
		SetupFREngine()
 
		' Process sample image
		ProcessImage()
	If Err.Number <> 0 Then
		DisplayMessage( Err.Description )
		Err.Clear()
	End If
End Sub
 
Sub SetupFREngine
	DisplayMessage( "Loading predefined profile..." )
	Engine.LoadPredefinedProfile( "DocumentConversion_Accuracy" )
	' Possible profile names are:
	'   "DocumentConversion_Accuracy", "DocumentConversion_Speed",
	'   "DocumentArchiving_Accuracy", "DocumentArchiving_Speed",
	'   "BookArchiving_Accuracy", "BookArchiving_Speed",
	'   "TextExtraction_Accuracy", "TextExtraction_Speed",
	'   "FieldLevelRecognition",
	'   "BarcodeRecognition",
	'   "Version9Compatibility",
	'   "Default"
End Sub
 
Sub ProcessImage
	Dim imagePath, rtfExportPath, pdfExportPath
	Dim FEF_RTF, FEF_PDF, PES_Balanced
 
	imagePath = GetSamplesFolder() & "\SampleImages\Demo.tif"
 
	' Don't recognize PDF file with a textual content, just copy it
	If Engine.IsPdfWithTextualContent( imagePath, Nothing ) Then
		DisplayMessage( "Copy results..." )
		resultPath = GetSamplesFolder() & "\SampleImages\Demo_copy.pdf"
		FSO.CopyFile imagePath, resultPath, true
		Exit Sub
	End If
 
	' Create document
	Set document = Engine.CreateFRDocument()
 
	On Error Resume Next
		' Add image file to document
		DisplayMessage( "Loading image..." )
		document.AddImageFile imagePath, Nothing, Nothing
 
		' Process document
		DisplayMessage( "Process..." )
		document.Process( Nothing )
 
		' Save results
		DisplayMessage( "Saving results..." )
		FEF_RTF = 0
		FEF_PDF = 4
		PES_Balanced = 1
 
		' Save results to rtf with default parameters
		rtfExportPath = GetSamplesFolder() & "\SampleImages\Demo.rtf"
		document.Export rtfExportPath, FEF_RTF, Nothing
 
		' Save results to pdf using 'balanced' scenario
		Set pdfParams = Engine.CreatePDFExportParams()
		pdfParams.Scenario = PES_Balanced
 
		pdfExportPath = GetSamplesFolder() & "\SampleImages\Demo.pdf"
		document.Export pdfExportPath, FEF_PDF, pdfParams
 
		' Close document
		document.Close()
	If Err.Number <> 0 Then
		DisplayMessage( Err.Description )
		Err.Clear()
 
		' Close document
		document.Close()
	End If
End Sub
 
Sub UnloadEngine
	DisplayMessage( "Deinitializing Engine..." )
	EngineLoader.ExplicitlyUnload()
End Sub
 
Sub DisplayMessage( message )
	WScript.StdOut.WriteLine( message )
End Sub


JavaScript

// © 2013 ABBYY Production LLC
// SAMPLES code is property of ABBYY, exclusive rights are reserved. 
//
// DEVELOPER is allowed to incorporate SAMPLES into his own APPLICATION and modify it under 
// the  terms of  License Agreement between  ABBYY and DEVELOPER.
 
 
// ABBYY FineReader Engine 11 Sample
 
// This sample shows basic steps of ABBYY FineReader Engine usage:
// Initializing, opening image file, recognition and export.
 
try {
	// Include config-file SamplesConfig.js
	var FSO = new ActiveXObject( "Scripting.FileSystemObject" );
	var ForReading = 1;
	var SamplesConfigFile = FSO.OpenTextFile( "..\\SamplesConfig.js", ForReading );
	eval( SamplesConfigFile.ReadAll() );
 
	var EngineLoader = null, Engine = null;
	Run();
} catch( error ) {
	DisplayMessage( error.description );
	WScript.Quit( -1 );
}
 
function Run()
{
	// Load ABBYY FineReader Engine
	LoadEngine();
	try{
		// Process with ABBYY FineReader Engine
		ProcessWithEngine();
	} finally {
		// Unload ABBYY FineReader Engine
		UnloadEngine();
	}
}
 
function LoadEngine()
{
	DisplayMessage( "Initializing Engine..." );
	EngineLoader = new ActiveXObject( "FREngine.OutprocLoader" );
	Engine = EngineLoader.GetEngineObject( GetDeveloperSN() );
}
 
function ProcessWithEngine()
{
	try {
		// Setup FREngine
		SetupFREngine();
 
		// Process sample image
		ProcessImage();
	} catch( error ) {
		DisplayMessage( error.description );
	}
}
 
function SetupFREngine()
{
	DisplayMessage( "Loading predefined profile..." );
	Engine.LoadPredefinedProfile( "DocumentConversion_Accuracy" );
	// Possible profile names are:
	//   "DocumentConversion_Accuracy", "DocumentConversion_Speed",
	//   "DocumentArchiving_Accuracy", "DocumentArchiving_Speed",
	//   "BookArchiving_Accuracy", "BookArchiving_Speed",
	//   "TextExtraction_Accuracy", "TextExtraction_Speed",
	//   "FieldLevelRecognition",
	//   "BarcodeRecognition",
	//   "Version9Compatibility",
	//   "Default"
}
 
function ProcessImage()
{
	var imagePath = GetSamplesFolder() + "\\SampleImages\\Demo.tif";
 
	// Don't recognize PDF file with a textual content, just copy it
	if( Engine.IsPdfWithTextualContent( imagePath, EngineLoader.NullObject ) ) {
		DisplayMessage( "Copy results..." );
		var resultPath = GetSamplesFolder() + "\\SampleImages\\Demo_copy.pdf";
		FSO.CopyFile( imagePath, resultPath, true );
		return;
	}
 
	// Create document
	var document = Engine.CreateFRDocument();
 
	try {
		// Add image file to document
		DisplayMessage( "Loading image..." );
		document.AddImageFile( imagePath, EngineLoader.NullObject, EngineLoader.NullObject );
 
		// Process document
		DisplayMessage( "Process..." );
		document.Process( EngineLoader.NullObject );
 
		// Save results
		DisplayMessage( "Saving results..." );
		var FEF_RTF = 0, FEF_PDF = 4, PES_Balanced = 1;
 
		// Save results to rtf with default parameters
		var rtfExportPath = GetSamplesFolder() + "\\SampleImages\\Demo.rtf";
		document.Export( rtfExportPath, FEF_RTF, EngineLoader.NullObject );
 
		// Save results to pdf using 'balanced' scenario
		var pdfParams = Engine.CreatePDFExportParams();
		pdfParams.Scenario = PES_Balanced;
 
		var pdfExportPath = GetSamplesFolder() + "\\SampleImages\\Demo.pdf";
		document.Export( pdfExportPath, FEF_PDF, pdfParams );
	} catch( error ) {
		DisplayMessage( error.description );
	} finally {
		// Close document
		document.Close();
	}
}
 
function UnloadEngine()
{
	DisplayMessage( "Deinitializing Engine..." );
	Engine = null;
	EngineLoader.ExplicitlyUnload();
	EngineLoader = null;
}
 
function DisplayMessage( message )
{
	WScript.StdOut.WriteLine( message );
}


Perl

# © 2013 ABBYY Production LLC
# SAMPLES code is property of ABBYY, exclusive rights are reserved. 
#
# DEVELOPER is allowed to incorporate SAMPLES into his own APPLICATION and modify it under 
# the  terms of  License Agreement between  ABBYY and DEVELOPER.
 
 
# ABBYY FineReader Engine 11 Sample
 
# This sample shows basic steps of ABBYY FineReader Engine usage:
# Initializing, opening image file, recognition and export.
 
use Win32::OLE::Variant qw( :DEFAULT nothing );
use File::Copy;
$EngineLoader;
$Engine;
 
eval {
	# Include config-file SamplesConfig.pl
	require '..\\SamplesConfig.pl';
 
	Run();
	1;
} or do {
	DisplayMessage( $@ );
	$@ = nothing;
	WScript.Quit( -1 );
};
 
sub Run
{
	# Load ABBYY FineReader Engine
	LoadEngine();
 
	# Process with ABBYY FineReader Engine
	ProcessWithEngine();
 
	# Unload ABBYY FineReader Engine
	UnloadEngine();
}
 
sub LoadEngine
{
	DisplayMessage( 'Initializing Engine...' );
	$EngineLoader = $WScript->CreateObject( 'FREngine.OutprocLoader' );
	$Engine = $EngineLoader->GetEngineObject( GetDeveloperSN() );
}
 
sub ProcessWithEngine
{
	eval {
		# Setup FREngine
		SetupFREngine();
 
		# Process sample image
		ProcessImage();
		1;
	} or do {
		DisplayMessage( $@ );
	};
}
 
sub SetupFREngine
{
	DisplayMessage( 'Loading predefined profile...' );
	$Engine->LoadPredefinedProfile( 'DocumentConversion_Accuracy' );
	# Possible profile names are:
	#   'DocumentConversion_Accuracy', 'DocumentConversion_Speed',
	#   'DocumentArchiving_Accuracy', 'DocumentArchiving_Speed',
	#   'BookArchiving_Accuracy', 'BookArchiving_Speed',
	#   'TextExtraction_Accuracy', 'TextExtraction_Speed',
	#   'FieldLevelRecognition',
	#   'BarcodeRecognition',
	#   'Version9Compatibility',
	#   'Default'
}
 
sub ProcessImage
{
	my $imagePath = GetSamplesFolder() . '\\SampleImages\\Demo.tif';
 
	# Don't recognize PDF file with a textual content, just copy it
	if( $Engine->IsPdfWithTextualContent( $imagePath, nothing ) ) {
		DisplayMessage( 'Copy results...' );
		my $resultPath = GetSamplesFolder() . '\\SampleImages\\Demo_copy.pdf';
		copy( $imagePath, $resultPath );
		return 1;
	}
 
	# Create document
	my $document = $Engine->CreateFRDocument();
 
	eval {
		# Add image file to document
		DisplayMessage( 'Loading image...' );
		$document->AddImageFile( $imagePath, nothing, nothing );
 
		# Process document
		DisplayMessage( 'Process...' );
		$document->Process( nothing );
 
		# Save results
		DisplayMessage( 'Saving results...' );
		my $FEF_RTF = 0, $FEF_PDF = 4, $PES_Balanced = 1;
 
		# Save results to rtf with default parameters
		my $rtfExportPath = GetSamplesFolder() . '\\SampleImages\\Demo.rtf';
		$document->Export( $rtfExportPath, $FEF_RTF, nothing );
 
		# Save results to pdf using 'balanced' scenario
		my $pdfParams = $Engine->CreatePDFExportParams();
		$pdfParams->put_Scenario( $PES_Balanced );
 
		my $pdfExportPath = GetSamplesFolder() . '\\SampleImages\\Demo.pdf';
		$document->Export( $pdfExportPath, $FEF_PDF, $pdfParams );
 
		# Close document
		$document->Close();
		1;
	} or do {
		DisplayMessage( $@ );
 
		# Close document
		$document->Close();
	};
}
 
sub UnloadEngine
{
	DisplayMessage( 'Deinitializing Engine...' );
	$Engine = nothing;
	$EngineLoader->ExplicitlyUnload();
	$EngineLoader = nothing;
}
 
sub DisplayMessage
{
	$WScript->StdOut->WriteLine( $_[0] );
}


Back To:

This website uses cookies which enable you to see pages or use other functions of our websites. You can turn off such cookies in your browser’s settings. If you continue to use these pages, you consent to the use of cookies.