private string DetermineDocumentType(string projectId, string imageFile, string classifierPath)
        {
            Console.WriteLine("Determine Document Type is processing ...");
            if (engine == null)
            {
                engine = LoadEngine(projectId);
            }
            if (processor == null)
            {
                processor = engine.CreateFlexiCaptureProcessor();
                processor.AddClassificationTreeFile(classifierPath);
                Console.WriteLine("Classifier template is added ...");
            }
            else
            {
                processor.ResetProcessing();
                Console.WriteLine("Processor is reseted");
            }
            processor.AddImageFile(@imageFile);
            var result = processor.ClassifyNextPage();

            if (result != null && result.PageType == PageTypeEnum.PT_MeetsDocumentDefinition)
            {
                var names = result.GetClassNames();
                Console.WriteLine("Determine Document Type is completed");
                return(names.Item(0));
            }


            Console.WriteLine("Determine Document Type is not completed");
            return("");
        }
Beispiel #2
0
        private bool tryProcessTask()
        {
            bool result = false;

            if (requestsInWork.Count == 0 && requests.Count != 0)
            {
                TryToAddRequestsToWork();
            }

            //take actual request, cut, process task from it and put this request back to queue.
            Request actualRequest;
            bool    actualRequestObtained = requestsInWork.TryDequeue(out actualRequest);

            if (actualRequestObtained)
            {
                result = true;
                List <byte[]> images;
                bool          isRequestPieceObtained = actualRequest.TryGetRequestPiece(taskSize, out images);
                if (isRequestPieceObtained)
                {
                    requestsInWork.Enqueue(actualRequest);

                    int     procNumb;
                    IEngine engine;
                    IFlexiCaptureProcessor processor = null;
                    processor = fcProcessorsPool.GetProcessor(out engine, out procNumb);

                    Tuple <IFlexiCaptureProcessor, IEngine, int, List <byte[]>, Request> startParams =
                        Tuple.Create(processor, engine, procNumb, images, actualRequest);
                    Thread recognition = new Thread(Recognition);
                    recognition.Start(startParams);
                }
            }
            return(result);
        }
        // USE CASE: Creating a Document Definition from a FlexiLayout (*.afl)
        public static void Creating_a_Document_Definition_from_a_FlexiLayout(IEngine engine)
        {
            trace("Create a Document Definition from an *.afl file...");
            string flexibleDescriptionFilePath = SamplesFolder + "\\SampleMisc\\Invoice_eng.afl";
            IDocumentDefinition newDefinition  = engine.CreateDocumentDefinitionFromAFL(flexibleDescriptionFilePath, "English");

            // You can save the new Document Definition to a file or use it from memory
            traceBegin("Use the Document Definition in FlexiCaptureProcessor...");
            IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();

            processor.AddDocumentDefinition(newDefinition);

            // Add images for a single document
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_1.tif");

            // Recognize the document and check the result
            IDocument document = processor.RecognizeNextDocument();

            assert(document != null);
            assert(document.DocumentDefinition != null);
            assert(document.Pages.Count == 1);

            // Export the result
            processor.ExportDocumentEx(document, SamplesFolder + "\\FCEExport", "Invoice", null);
            traceEnd("OK");
        }
        // USE CASE: Creating a compound Document Definition
        public static void Creating_a_compound_Document_Definition(IEngine engine)
        {
            trace("Create an empty Document Definition in memory...");
            IDocumentDefinition newDefinition = engine.CreateDocumentDefinition();

            assert(newDefinition != null);

            trace("Set default language...");
            ILanguage language = engine.PredefinedLanguages.FindLanguage("English");

            assert(language != null);
            newDefinition.DefaultLanguage = language;

            trace("Create a new fixed section from an XFD file...");
            newDefinition.DefaultTextType = TextTypeEnum.TT_Handprinted;
            ISectionDefinition newSection1 = newDefinition.Sections.AddNew("Banking");

            newSection1.LoadXFDDescription(SamplesFolder + "\\SampleMisc\\Banking_eng.xfd");

            trace("Create a new flexible section from an AFL file...");
            newDefinition.DefaultTextType = TextTypeEnum.TT_Normal;
            ISectionDefinition newSection2 = newDefinition.Sections.AddNew("Invoice");

            newSection2.LoadFlexibleDescription(SamplesFolder + "\\SampleMisc\\Invoice_eng.afl");

            // Modify the template as required. In this sample we need to loosen some constraints
            IPageAnalysisParams analysisParams = engine.CreatePageAnalysisParams();

            analysisParams.CopyFrom(newDefinition.PageAnalysisParams);
            analysisParams.MaxHorizontalShrinkPercent = 20;
            analysisParams.MaxVerticalShrinkPercent   = 20;
            newDefinition.PageAnalysisParams          = analysisParams;

            trace("Check the Document Definition...");
            assert(newDefinition.Check() == true);

            // You can save the new Document Definition to a file or use it from memory
            traceBegin("Use the Document Definition in FlexiCaptureProcessor...");
            IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();

            processor.AddDocumentDefinition(newDefinition);

            // Add images for a single multipage document
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Banking_1.tif");
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Banking_2.tif");
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Banking_3.tif");
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_2.tif");
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_3.tif");

            // Recognize the document
            IDocument document = processor.RecognizeNextDocument();

            assert(document != null);
            assert(document.DocumentDefinition != null);
            assert(document.Pages.Count == 5);

            processor.ExportDocumentEx(document, SamplesFolder + "\\FCEExport", "Mixed", null);
            traceEnd("OK");
        }
Beispiel #5
0
        internal static void DoConfigureProcessor(string docDifPath, IFlexiCaptureProcessor processor, IEngine engine)
        {
            IDocumentDefinition documentDefinition = createDocDifinitionFromXML(docDifPath, engine);
            bool checkresult = documentDefinition.Check();

            IStringsCollection errors = documentDefinition.Errors;

            for (int i = 0; i < errors.Count; i++)
            {
                string str = errors[i];
            }

            bool isvalid = documentDefinition.IsValid;

            processor.AddDocumentDefinition(documentDefinition);
            processor.SetForceApplyDocumentDefinition(true);
        }
        // HOW-TO: Explicitly release a COM object from managed code
        public static void How_to_explicitly_release_a_COM_object_from_managed_code(IEngine engine)
        {
            // When using COM objects from managed code, it is sometimes
            // required to ensure that a COM object is released at a specific point
            // and not left to the garbage collector (which might not
            // release the object for a long time). This might be needed to release
            // memory or other resources taken by the object if there is no other
            // way to release them. This is effectively "DISPOSAL" of the COM object and
            // the same rules should be followed as those used when deciding if
            // Dispose method should be called on a disposable .NET Framework object.

            // Releasing resources in timely manner is especially important in
            // server applications. Garbage collector does not "know" the real
            // amount of memory and resources used by COM objects and will
            // not "know" that they should be garbage-collected. As the result, your server
            // application might run low on non-managed memory while managed heap
            // will be still almost empty and garbage-collection will not run.

            // For example, the FlexiCapture processor object has some internal state,
            // which consumes resources. If we just leave the object to the garbage
            // collector, we cannot be sure when these resources are released.
            // It might not be a problem in most cases, but if our application
            // is to be used in high-performance scenarios we'd be better off
            // explicitly releasing the object. The downside of this scenario is
            // the complexity of the resulting code.

            trace("Create a COM object...");
            IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();

            try {
                // Some processing here
                // ...
            } finally {
                trace("To explicitly release the COM object, call Marshal.ReleaseComObject.");
                // We are sure that this instance of the processor will never be used again,
                // so we are safe to release it here
                Marshal.ReleaseComObject(processor);
            }

            // NB. Nulling the object WILL NOT do the trick as it did in Visual Basic 6.0.
            // Calling GC.Collect might SEEM to work under some conditions but does not
            // guarantee that a particular object will be freed (a stray reference).
            // Moreover, GC.Collect might be a very lengthy operation (if your managed heap
            // contains many objects) and calling it often might significantly degrade performance.
        }
        public MainForm(string workDirectory, string fileLoad)
        {
            InitializeComponent();

            // инициализация движка ABBYY
            try
            {
                _engineLoader = new FCEngine.InprocLoader
                {
                    CustomerProjectId = FceConfig.GetCustomerProjectId(), LicensePassword = "", LicensePath = ""
                };
                _engine = _engineLoader.GetEngine();

                //engine = engineLoader.Load(FceConfig.GetCustomerProjectId(), "");
                _processor = _engine.CreateFlexiCaptureProcessor();
                // Initialize DocumentView component
                documentView.Engine = _engine;

                _pathFolderWork = workDirectory;

                if (_pathFolderWork.EndsWith("\\"))
                {
                    _pathFolderWork = _pathFolderWork.Substring(0, _pathFolderWork.Length - 1);
                }

                if (String.IsNullOrEmpty(Path.GetDirectoryName(_pathFolderWork)))
                {
                    throw new Exception(_pathFolderWork + " не является корректной директорией");
                }

                _pathFolderWork += "\\visualeditorfiles\\";
                if (!String.IsNullOrEmpty(fileLoad))
                {
                    _nameCurrentFile = Path.GetFileNameWithoutExtension(fileLoad);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Не удалось загрузить компоненту ABBYY по причине: " + ex.Message);
                Application.Exit();
            }
            //updateButtonsState();
            LoadWorkDocument();
        }
        static IDocument PrepareNewRecognizedDocument(IEngine engine, out IFlexiCaptureProcessor processor)
        {
            // Create and configure an instance of FlexiCapture processor
            processor = engine.CreateFlexiCaptureProcessor();
            processor.AddDocumentDefinitionFile(SamplesFolder + "\\SampleProject\\Templates\\Invoice_eng.fcdot");

            // Add images for a single multipage document
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_2.tif");
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_3.tif");

            // Recognize the document
            IDocument document = processor.RecognizeNextDocument();

            assert(document != null);
            assert(document.DocumentDefinition != null);
            assert(document.Pages.Count == 2);

            return(document);
        }
        // USE CASE: Using image processing tools in custom preprocessing
        public static void Using_image_processing_tools_in_custom_preprocessing(IEngine engine)
        {
            trace("Create and configure an instance of FlexiCapture processor...");
            IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();

            processor.AddDocumentDefinitionFile(SamplesFolder + "\\SampleProject\\Templates\\Invoice_eng.fcdot");

            trace("Set up an image source with custom preprocessing...");
            // Create and configure sample image source. ALL PREPROCESSING IS DONE IN THE IMAGE SOURCE
            // (see SampleImageSource class for details)
            CustomPreprocessingImageSource imageSource = new CustomPreprocessingImageSource(engine);

            imageSource.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_1.tif");
            imageSource.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_2.tif");
            imageSource.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_3.tif");
            processor.SetCustomImageSource(imageSource);

            traceBegin("Process the images...");
            int count = 0;

            while (true)
            {
                IDocument document = processor.RecognizeNextDocument();
                if (document == null)
                {
                    IProcessingError error = processor.GetLastProcessingError();
                    assert(error == null);                       // No errors are expected in this sample
                    break;
                }
                else
                {
                    // We expect that this will never happen in this sample
                    assert(document.DocumentDefinition != null);
                }
                processor.ExportDocumentEx(document, SamplesFolder + "\\FCEExport", "NextDocument_" + count, null);
                count++;
            }
            traceEnd("OK");

            trace("Check the results...");
            assert(count == 2);
        }
        static void CheckTrainedDocumentDefinition(IEngine engine, IDocumentDefinition newDocumentDefinition, string rootFolder)
        {
            IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();

            processor.AddDocumentDefinition(newDocumentDefinition);
            processor.AddImageFile(rootFolder + "\\02.jpg");
            processor.AddImageFile(rootFolder + "\\03.jpg");

            IDocument document = processor.RecognizeNextDocument();

            assert(document.DocumentDefinition != null);
            assert(document.Pages.Count == 1);
            assert(document.Sections[0].Children[0].Name == "ISBN");
            assert(document.Sections[0].Children[0].Value.AsString == "0-517-59939-2");

            document = processor.RecognizeNextDocument();
            assert(document.DocumentDefinition != null);
            assert(document.Pages.Count == 1);
            assert(document.Sections[0].Children[0].Name == "ISBN");
            assert(document.Sections[0].Children[0].Value.AsString == "0-8050-6176-2");
        }
        // USE CASE: Creating a Document Definition from an XML Form Definition
        public static void Creating_a_Document_Definition_from_an_XML_Form_Definition(IEngine engine)
        {
            trace("Create a Document Definition from an *.xfd file...");
            string formDescriptionFilePath    = SamplesFolder + "\\SampleMisc\\Banking_eng.xfd";
            IDocumentDefinition newDefinition = engine.CreateDocumentDefinitionFromXFD(formDescriptionFilePath, "English");

            // Modify the template as required. In this sample we need to loosen some constraints
            IPageAnalysisParams analysisParams = engine.CreatePageAnalysisParams();

            analysisParams.CopyFrom(newDefinition.PageAnalysisParams);
            analysisParams.MaxHorizontalShrinkPercent = 20;
            analysisParams.MaxVerticalShrinkPercent   = 20;
            newDefinition.PageAnalysisParams          = analysisParams;

            // You can save the new Document Definition to a file or use it from memory
            traceBegin("Use the Document Definition in FlexiCaptureProcessor...");
            IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();

            processor.AddDocumentDefinition(newDefinition);

            // Add images for a single multipage document
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Banking_1.tif");
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Banking_2.tif");
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Banking_3.tif");

            // Recognize the document and check the result
            IDocument document = processor.RecognizeNextDocument();

            assert(document != null);
            assert(document.DocumentDefinition != null);
            assert(document.Pages.Count == 3);

            // Export the result
            processor.ExportDocumentEx(document, SamplesFolder + "\\FCEExport", "Banking", null);
            traceEnd("OK");
        }
Beispiel #12
0
        public static Tuple <Dictionary <string, object>, List <object> > GetData(string imgPath)
        {
            if (engine == null)
            {
                engine = loadEngine();
            }
            if (processor == null)
            {
                processor = engine.CreateFlexiCaptureProcessor();
                processor.AddDocumentDefinitionFile("C:\\ProgramData\\ABBYY\\SDK\\11\\FlexiCapture Engine\\Samples\\SampleMisc\\Invoice.fcdot");
            }
            else
            {
                processor.ResetProcessing();
            }
            processor.AddImageFile(imgPath);
            IDocument document = processor.RecognizeNextDocument();

            if (document != null && document.DocumentDefinition != null)
            {
                return(ReadDocData(document));
            }
            return(null);
        }
        static IDocument PrepareNewRecognizedDocument( IEngine engine, out IFlexiCaptureProcessor processor )
        {
            // Create and configure an instance of FlexiCapture processor
            processor = engine.CreateFlexiCaptureProcessor();
            processor.AddDocumentDefinitionFile( SamplesFolder + "\\SampleProject\\Templates\\Invoice_eng.fcdot" );

            // Add images for a single multipage document
            processor.AddImageFile( SamplesFolder + "\\SampleImages\\Invoices_2.tif" );
            processor.AddImageFile( SamplesFolder + "\\SampleImages\\Invoices_3.tif" );

            // Recognize the document
            IDocument document = processor.RecognizeNextDocument();
            assert( document != null );
            assert( document.DocumentDefinition != null );
            assert( document.Pages.Count == 2 );

            return document;
        }
        // USE CASE: Configuring fields for better recognition results
        public static void Configuring_fields_for_better_recognition_results(IEngine engine)
        {
            trace("Create a Document Definition from a FlexiLayout...");
            IDocumentDefinition newDefinition = engine.CreateDocumentDefinitionFromAFL(SamplesFolder + "\\SampleMisc\\Invoice_eng.afl", "English");

            assert(newDefinition != null);

            trace("Configure data types...");
            setFieldValueType(newDefinition, "InvoiceDate", FieldValueTypeEnum.FVT_DateTime);
            setFieldValueType(newDefinition, "Quantity", FieldValueTypeEnum.FVT_Number);
            setFieldValueType(newDefinition, "UnitPrice", FieldValueTypeEnum.FVT_Currency);
            setFieldValueType(newDefinition, "Total", FieldValueTypeEnum.FVT_Currency);
            setFieldValueType(newDefinition, "TotalAmount", FieldValueTypeEnum.FVT_Currency);

            trace("Configure recognition languages for text fields ...");
            IFieldDefinition fieldDef = findFieldDef(newDefinition, "InvoiceNumber");

            assert(fieldDef != null);
            ITextRecognitionParams textParams = fieldDef.RecognitionParams.AsTextParams();
            ILanguage newLanguage             = textParams.CreateEmbeddedLanguageByDataType(FieldValueTypeEnum.FVT_DateTime);

            textParams.Language = newLanguage;

            newLanguage = textParams.CreateEmbeddedLanguage(textParams.Language.Type, textParams.Language);
            assert(newLanguage != textParams.Language);
            assert(newLanguage.LanguageCategory == LanguageCategoryEnum.LC_DataType);
            assert(newLanguage.DatatypeCategory == DatatypeCategoryEnum.TC_DateTime);
            textParams.Language = newLanguage;

            newLanguage = textParams.CreateEmbeddedLanguage(LanguageTypeEnum.LT_Group, null);
            newLanguage.AsGroupLanguage().Add(engine.PredefinedLanguages.FindLanguage("English"));
            newLanguage.AsGroupLanguage().Add(engine.PredefinedLanguages.FindLanguage("Russian"));
            textParams.Language = newLanguage;
            assert(textParams.Language.Type == LanguageTypeEnum.LT_Group);
            assert(textParams.Language.AsGroupLanguage().Count == 2);
            assert(textParams.Language.AsGroupLanguage().Item(0).InternalName == "English");
            assert(textParams.Language.AsGroupLanguage().Item(1).InternalName == "Russian");

            newLanguage = textParams.CreateEmbeddedLanguage(LanguageTypeEnum.LT_Simple, null);
            newLanguage.AsSimpleLanguage().set_LetterSet(LanguageLetterSetEnum.LLS_Alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            newLanguage.AsSimpleLanguage().RegularExpression = "[A-Z]{1-}";
            textParams.Language = newLanguage;
            assert(textParams.Language.AsSimpleLanguage().RegularExpression.Length > 0);

            newLanguage = textParams.CreateEmbeddedLanguage(LanguageTypeEnum.LT_Simple, engine.PredefinedLanguages.FindLanguage("English"));
            assert(newLanguage.AsSimpleLanguage().UsePredefinedDictionary == true);
            assert(newLanguage.AsSimpleLanguage().UseUserDefinedDictionary == false);
            assert(newLanguage.AsSimpleLanguage().UserDefinedDictionary == null);
            newLanguage.AsSimpleLanguage().UseUserDefinedDictionary = true;
            FCEngine.IDictionary dictionary = newLanguage.AsSimpleLanguage().UserDefinedDictionary;
            assert(dictionary != null);
            assert(dictionary.WordsCount == 0);
            dictionary.AddWord("ONE", 1);
            dictionary.AddWord("TWO", 1);
            dictionary.AddWord("THREE", 1);
            assert(dictionary.WordsCount == 3);
            IEnumDictionaryWords enumWords = dictionary.EnumWords();

            for (int i = 0; i < 10; i++)
            {
                int    confidence = 0;
                string word       = enumWords.Next(out confidence);
                if (confidence == 0)
                {
                    break;
                }
                trace(word);
            }
            textParams.Language = newLanguage;

            trace("Check the Document Definition...");
            assert(newDefinition.Check() == true);

            traceBegin("Use the Document Definition in FlexiCaptureProcessor...");
            IFlexiCaptureProcessor processor = engine.CreateFlexiCaptureProcessor();

            processor.AddDocumentDefinition(newDefinition);

            // Add images for a single document
            processor.AddImageFile(SamplesFolder + "\\SampleImages\\Invoices_1.tif");

            // Recognize the document
            IDocument document = processor.RecognizeNextDocument();

            assert(document != null);
            assert(document.DocumentDefinition != null);
            assert(document.Pages.Count == 1);

            processor.ExportDocumentEx(document, SamplesFolder + "\\FCEExport", "Invoice", null);
            traceEnd("OK");
        }
        static void processError( IProcessingError error, IFlexiCaptureProcessor processor, ErrorHandlingStrategy strategy )
        {
            assert( error != null );

            // You can store the problematic image for later analysis
            if( error.ImageFile() != null ) {
                string imageReference = error.ImageFile().GetFileReference();
            }

            // And use different strategies to handle the error
            switch( strategy ) {
                // 1) Log the error and continue
                case ErrorHandlingStrategy.LogAndContinue:
                    trace( "Processing error: " + error.MessageText() );
                    return;
                // 2) Retry
                case ErrorHandlingStrategy.LogAndRetry:
                    trace( "Processing error: " + error.MessageText() );
                    // Resume processing from the position where error occurred
                    processor.ResumeProcessing( true );
                    return;
                // 3) Break processing
                case ErrorHandlingStrategy.ThrowException:
                    // This will reset the processing state (image queue, error condition, etc.) but keep
                    // the processing configuration (loaded templates and processing params). You can omit this
                    // call if you do not plan to reuse the processor instance or reset the processor elsewhere
                    processor.ResetProcessing();
                    throw new Exception( error.MessageText() );
            }
        }
Beispiel #16
0
        protected override void Execute(CodeActivityContext context)
        {
            string            message           = "";
            List <LogMessage> logList           = new List <LogMessage>();
            List <string>     nonInvoices       = new List <string>();
            List <string>     notConfidenceList = new List <string>();

            message = "Loading FlexiCapture Engine for Recognition ...";
            Console.WriteLine(message);
            logList.Add(new LogMessage(message, LogType.Information));

            string documentProjectId = DocumentProjectId.Get(context);

            message = "Get Document Project ID: " + documentProjectId;
            Console.WriteLine(message);
            logList.Add(new LogMessage(message, LogType.Information));

            int confidenceLevelLimitation = ConfidenceLevelLimitation.Get(context);

            message = string.Format("Confidence Level Limitation: {0}", confidenceLevelLimitation);
            Console.WriteLine(message);
            logList.Add(new LogMessage(message, LogType.Information));

            string sourceFolder = SourceFolder.Get(context);

            message = string.Format("Get PDF folder: {0}", sourceFolder);
            Console.WriteLine(message);
            logList.Add(new LogMessage(message, LogType.Information));

            string ocrTemplate = OCRTemplateFolder.Get(context);

            message = string.Format("OCR Template Folder: {0}", ocrTemplate);
            Console.WriteLine(message);
            logList.Add(new LogMessage(message, LogType.Information));

            string exportFolder = ExportFolder.Get(context);

            message = string.Format("Get Export Folder: {0}", exportFolder);
            Console.WriteLine(message);
            logList.Add(new LogMessage(message, LogType.Information));
            if (!Directory.Exists(exportFolder))
            {
                Directory.CreateDirectory(exportFolder);
                message = string.Format("Folder {0} is created completely", exportFolder);
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));
            }

            string recognizedFolder = RecognizedFolder.Get(context);

            Console.WriteLine("Get Recognize Folder: " + recognizedFolder);
            logList.Add(new LogMessage("Recognize Folder is:  " + recognizedFolder, LogType.Information));
            if (!Directory.Exists(recognizedFolder))
            {
                Directory.CreateDirectory(recognizedFolder);
                message = string.Format("Folder {0} is created completely", recognizedFolder);
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));
            }

            string notConfidenceFolder = NotConfidenceFolder.Get(context);

            Console.WriteLine("Get Not Confidence Folder: " + notConfidenceFolder);
            logList.Add(new LogMessage("Not Confidence Folder is:  " + notConfidenceFolder, LogType.Information));
            if (!Directory.Exists(notConfidenceFolder))
            {
                Directory.CreateDirectory(notConfidenceFolder);
                message = string.Format("Folder {0} is created completely", notConfidenceFolder);
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));
            }

            string rejectFolder = RejectFolder.Get(context);

            Console.WriteLine("Get Reject Folder: " + rejectFolder);
            logList.Add(new LogMessage("Reject Folder is:  " + rejectFolder, LogType.Information));
            if (!Directory.Exists(rejectFolder))
            {
                Directory.CreateDirectory(rejectFolder);
                message = string.Format("Folder {0} is created completely", rejectFolder);
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));
            }

            engine = LoadEngine(documentProjectId);

            Boolean isRecognized    = false;
            bool    isValidInvoice  = false;
            int     count           = 0;
            int     noError         = 0;
            int     noSuccess       = 0;
            int     noNotConfidence = 0;

            try
            {
                message = "Creating and configuring the FlexiCapture Processor...";
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));

                processor = engine.CreateFlexiCaptureProcessor();

                message = "Adding Document Definition to process...";
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));
                string[] TotalOCRFiles = Directory.GetFiles(ocrTemplate, "*.fcdot", SearchOption.AllDirectories);
                if (TotalOCRFiles.Length == 0)
                {
                    message = string.Format("OCR Template *.fcdot is not found in the folder {0}", ocrTemplate);
                    Console.WriteLine(message);
                    logList.Add(new LogMessage(message, LogType.Error));
                    throw new Exception(message);
                }
                foreach (string ocr in TotalOCRFiles)
                {
                    processor.AddDocumentDefinitionFile(ocr);
                    message = string.Format("OCR Temhplate {0} is added", ocr);
                    Console.WriteLine(message);
                    logList.Add(new LogMessage(message, LogType.Information));
                }

                message = string.Format("Adding images to process...");
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));
                string[] TotalFiles = Directory.GetFiles(sourceFolder, "*.pdf", SearchOption.AllDirectories);
                if (TotalFiles.Length == 0)
                {
                    message = string.Format("PDF Files *.pdf is not found in the folder {0}.", sourceFolder);
                    Console.WriteLine(message);
                    logList.Add(new LogMessage(message, LogType.Error));
                    throw new Exception(message);
                }
                foreach (string pdfFile in TotalFiles)
                {
                    processor.AddImageFile(pdfFile);
                    message = string.Format("PDF Files {0} is added", pdfFile);
                    Console.WriteLine(message);
                    logList.Add(new LogMessage(message, LogType.Information));
                }

                message = "Recognizing the images and exporting the results...";
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));

                while (true)
                {
                    // Recognize next document
                    confidenceLevel        = 0;
                    confidenceDetailLevel  = 0;
                    confidenceHeaderLevel  = 0;
                    totalConfidenceLevelHD = 0;
                    IDocument document = processor.RecognizeNextDocument();
                    if (document == null)
                    {
                        IProcessingError error = processor.GetLastProcessingError();
                        if (error != null)
                        {
                            // Processing error
                            message = string.Format("processing error because of {0}.", error.MessageText());
                            Console.WriteLine(message);
                            logList.Add(new LogMessage(message, LogType.Error));
                            continue;
                        }
                        else
                        {
                            // No more images
                            message = string.Format("all PDF Files has been executed or no PDF file in the folder {0}", sourceFolder);
                            Console.WriteLine(message);
                            logList.Add(new LogMessage(message, LogType.Information));
                            break;
                        }
                    }
                    else if (document.DocumentDefinition == null)
                    {
                        // Couldn't find matching template for the image. In this sample this is an error.
                        // In other scenarios this might be normal
                        message = string.Format("PDF file is not matched with existing OCR Templates.");
                        Console.WriteLine(message);
                        logList.Add(new LogMessage(message, LogType.Error));
                        //string tempPage = document.Pages[0].OriginalImagePath;
                        //if(tempPage != null)
                        //{
                        //    string movefile = Path.GetFileName(tempPage);
                        //    string tempfilename = Path.GetFileNameWithoutExtension(tempPage);

                        //    logList.Add(new LogMessage("Move to Reject Folder", LogType.Information));
                        //    if (File.Exists(tempPage))
                        //    {

                        //        MoveFileToDestinationFolder(sourceFolder, rejectFolder, tempPage, tempfilename);
                        //    }
                        //    else
                        //    {
                        //        MoveFileToDestinationFolder(notConfidenceFolder, rejectFolder, notConfidenceFolder+"\\"+movefile, tempfilename);
                        //    }

                        //}

                        continue;
                    }

                    string originalPath             = document.Pages[0].OriginalImagePath;
                    string file                     = Path.GetFileName(originalPath);
                    string filenamewithoutextention = Path.GetFileNameWithoutExtension(originalPath);

                    message = string.Format("Recognizing pdf {0} is started.", originalPath);
                    Console.WriteLine(message);
                    logList.Add(new LogMessage(message, LogType.Information));

                    //set confident level and status
                    message = string.Format("Extracting data from pdf {0} is started", originalPath);
                    Console.WriteLine(message);
                    logList.Add(new LogMessage(message, LogType.Information));
                    for (int i = 0; i < document.Sections.Count; i++)
                    { // extracing
                        var section = document.Sections[i];
                        if (object.ReferenceEquals(section, null))
                        {
                            continue;
                        }



                        for (int d = 0; d < section.Children.Count; d++)
                        {
                            var child = section.Children[d];
                            if (object.ReferenceEquals(child, null))
                            {
                                continue;
                            }

                            var field = ((IField)child);

                            message = string.Format("Extracting column {0} = {1}", field.Name, field.Value.AsText);
                            Console.WriteLine(message);

                            if (field.Name.ToUpper().Trim() == "INV_CONFIDENCE_LEVEL")
                            {
                                var value = TextFieldHelper.GetConfidenLevel(engine, document);
                                totalConfidenceLevelHD = value;
                                var data = engine.CreateText(value.ToString(), null);
                                field.Value.AsInteger = value;
                                confidenceLevel       = value;

                                message = string.Format("Confidence level of {0} is {1}", file, value);
                                Console.WriteLine(message);
                                logList.Add(new LogMessage(message, LogType.Information));
                            }

                            if (field.Name.ToUpper().Trim() == "INV_STATUS")
                            {
                                var value = "Recognized";
                                var data  = engine.CreateText(value, null);
                                field.Value.AsText.Delete(0, field.Value.AsText.Length);
                                field.Value.AsText.Insert(data, 0);
                            }


                            if (field.Name.ToUpper().Trim() == "FILE_NAME")
                            {
                                var data = engine.CreateText(file, null);
                                field.Value.AsText.Delete(0, field.Value.AsText.Length);
                                field.Value.AsText.Insert(data, 0);
                            }
                        }
                    } //end extracting
                    isValidInvoice = true;
                    if (isValidInvoice)
                    {
                        message = string.Format("Total Confidence is {0} ", totalConfidenceLevelHD);
                        Console.WriteLine(message);
                        logList.Add(new LogMessage(message, LogType.Information));
                        //check confidence level validation
                        if (totalConfidenceLevelHD >= confidenceLevelLimitation)
                        {
                            try
                            {
                                message = string.Format("Exporting process for pdf {0} is started ...", file);
                                Console.WriteLine(message);
                                logList.Add(new LogMessage(message, LogType.Information));

                                //IFileExportParams exportParams = engine.CreateFileExportParams();
                                //Console.WriteLine("XLS");
                                //exportParams.FileFormat = FileExportFormatEnum.FEF_XLS;


                                processor.ExportDocument(document, exportFolder);
                                Console.WriteLine("Exporting process is completed ...");

                                MoveFileToDestinationFolder(sourceFolder, exportFolder, originalPath, filenamewithoutextention);
                                Console.WriteLine(string.Format("Moving {0} to Export folder {1} is completed", file, exportFolder));

                                logList.Add(new LogMessage("Exporting process is ended ...", LogType.Information));
                                noSuccess++;
                            }
                            catch (Exception e)
                            {
                                noError++;
                                Console.WriteLine(string.Format("exporting is failed because of {0}.", e.Message));
                                logList.Add(new LogMessage(message, LogType.Error));

                                MoveFileToDestinationFolder(sourceFolder, rejectFolder, originalPath, filenamewithoutextention);
                                message = string.Format("Moving pdf {0} to Reject folder {1} is completed", file, rejectFolder);
                                Console.WriteLine(message);
                                logList.Add(new LogMessage(message, LogType.Information));
                                continue;
                            }
                        }
                        else
                        {
                            message = string.Format("Confidence of PDF {0} is {1} less than target confidence {2}", file, totalConfidenceLevelHD, confidenceLevelLimitation);
                            logList.Add(new LogMessage(message, LogType.Error));
                            notConfidenceList.Add(file);

                            logList.Add(new LogMessage(string.Format("Total number of not confidence is {0}", notConfidenceList.Count), LogType.Information));
                            MoveFileToDestinationFolder(sourceFolder, notConfidenceFolder, originalPath, filenamewithoutextention);
                            message = string.Format("Moving pdf {0} to Not Confidence folder {1} is completed", file, notConfidenceFolder);
                            Console.WriteLine(message);
                            logList.Add(new LogMessage(message, LogType.Information));
                            noNotConfidence++;
                        }
                    }
                    count++;
                }

                var msg = processor.GetLastProcessingError();
                if (msg != null)
                {
                    var msgError = string.Format("the processing error because of {0}.", msg.MessageText());
                    Console.WriteLine(msgError);
                    logList.Add(new LogMessage(msgError, LogType.Error));
                    noError++;
                }
                message = string.Format("No. of Not Confidence {0} and No. of Error {1} and No. of Exported to DB {2}", noNotConfidence, noError, noSuccess);
                logList.Add(new LogMessage(message, LogType.Information));
                isRecognized = true;
            }
            finally
            {
                UnloadEngine(ref engine);
                message = string.Format("Released FlexiCapture Engine for Recognition...");
                Console.WriteLine(message);
                logList.Add(new LogMessage(message, LogType.Information));

                IsRecognized.Set(context, isRecognized);
                NonInvoiceList.Set(context, nonInvoices);
                Messages.Set(context, logList);
                TotalExported.Set(context, noSuccess);
                TotalError.Set(context, noError);
                TotalNotConfidence.Set(context, noNotConfidence);
                NotConfidenceList.Set(context, notConfidenceList);
            }
        }