Code: Select all
static void ProcessTemplateDotNet()
{
string inputFile = @"C:\Temp\Template Samples\w2-example.jpg";
string templateLib = @"C:\Temp\Template Samples\Templates.tlz";
string outputFile = @"C:\Temp\Output.xml";
int conf = 0;
int matching = 0;
if (File.Exists(outputFile))
File.Delete(outputFile);
//Initialize Engine
Engine.Init(null, null);
//Create a settings collection
//Load the template library.
//Load the input file
using(SettingCollection sett = new SettingCollection())
using (FormTemplateLibrary formLibrary = new FormTemplateLibrary(templateLib, sett))
using (ImageFile inputImageFile =
new ImageFile(inputFile, FILEOPENMODE.IMGF_READ, IMF_FORMAT.FF_SIZE, sett))
{
//Modify any recognition settings
sett.DTXTOutputformat = DTXTOUTPUTFORMATS.DTXT_XMLCOORD;
//Iterate through the pages
for (int i = 0; i < inputImageFile.PageCount; i++)
{
//Load a page from the input file
using (Page page = new Page(inputImageFile, i, sett))
{
FormTemplateCollection templateCollection = null;
//Pre-process the document (rotate, deskew, etc.)
page.Preprocess();
//Find any templates that match thie input page
MatchingFormTemplate matchingTemplate =
page.FindFormTemplate(formLibrary, out templateCollection, out conf, out matching);
//Get detailed information about the match, useful for debugging.
string findInfo = templateCollection.FindInfo;
if (matching != 0)
{
//Apply the matching template to the page.
page.ApplyFormTemplate(matchingTemplate);
//Recognize (OCR) the page.
page.Recognize(outputFile);
//Iterate through the recognized zones
foreach (OCRZone zone in page.OCRZones)
{
Console.WriteLine("Name: {0} \n\rValue: {1}",
zone.Name, zone.Text);
}
}
//Free the template collection
if (templateCollection != null)
templateCollection.Dispose();
}
}
}
//Close the engine.
Engine.ForceQuit();
}