private bool ReadFileData(string filename) { // // If PDF and scanning method is image, then PDF needs to be split into separate images for each page // These individual pages then need to be scanned in using the Acusoft toolset (Syncfusion does not support images, only PDF image source files) //// Splid PDF into pages ////if (Path.GetExtension(fileStatus.fileName).ToLower() == ".pdf") ////{ //// // Process PDF file ready for scanning - split pages and export multiple .jpg. //// // This is currently dummy code, not yet implimented. See samples from Syncfusion on how to complete, if required //// var zz = ProcessPDF.Main(fileStatus.fileName); ////} ScanPageStatus scanPageStatus = new ScanPageStatus(); ProcessImage processImage = new ProcessImage(); scanPageStatus = processImage.ReadFile(filename); if (scanPageStatus.rc < 0) { logger.Error(scanPageStatus.statusMessage); return(false); } if (scanPageStatus.rc == 0) { logger.Info(scanPageStatus.statusMessage); } else { logger.Warn(scanPageStatus.statusMessage); } return(true); }
//readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // GET: Invoices public ActionResult Index() { // Log to database FileStatus fileStatus = new FileStatus(); ScanPageStatus scanPageStatus = new ScanPageStatus(); FileProcessor fileProcessor = new FileProcessor(); //fileStatus.rc = 0; while (fileStatus.rc <= 0) { fileStatus = fileProcessor.CheckNextFile(); // If the file is incorrect it will be moved to error directory automatically GPA *** move all functions to the same place! if (CheckFileStatus(fileStatus)) { // Scan the file reading in any data available if (ReadFileData(fileStatus.fileName)) { // If this is positive, then should have // - identified the company // - scanned in and returned the page with 'key' data (as a string) // Now need to interprit this data and write to the database for further checking / uploading fileProcessor.MoveFile(Path.GetFileName(fileStatus.fileName), "processed"); CheckAndReadScannedData(); } else { fileProcessor.MoveFile(Path.GetFileName(fileStatus.fileName), "error"); } } else { // Error - log message } } //FileProcessor.MoveFile() var invoices = db.Invoices.Include(i => i.Customer).Include(i => i.Supplier); return(View(invoices.ToList())); }
public ScanPageStatus ReadFile(string filename) { // Look for file in common location // move to in process // scan // move to processed (archive) // Need to have multiple pages available ScanPageStatus scanPageStatus = new ScanPageStatus(); try { // Database connection via DocumentScanningLibrary, this is intermediate table for data loads ScannedRecord scannedRecord = new ScannedRecord(); // { CompanyName = "test", CustomerNumber = 1, InvoiceNumber = 1 }; ////var test = db.ScannedRecords.Count(); ////db.ScannedRecords.InsertOnSubmit(scannedRecord); ////db.SubmitChanges(); if (!String.IsNullOrEmpty(filename)) { // ScanPDF useses the Tesseract public domain OCR tool set acting in PDF via Syncfusion toolset switch (Path.GetExtension(filename).ToLower()) { case ".pdf": ScanPDF scanPDF = new ScanPDF(); scanPageStatus = scanPDF.Process(filename, ref scannedRecord); //ScanPDF.Process(filename); break; case ".jpg": case ".tiff": // Scan Page uses the Accusoft.SmartZoneOCRSdk to process .jpg source files. // Needs to return "scanPageStatus, if used in the future var result = ScanImage.Main(filename); //if (result.ToString().Length > 0) {} break; default: break; } if (scanPageStatus.rc == 0) { // Persist record in the database, note in alert log if there are failed scans if (scannedRecord.FailedMatches == 0) { scanPageStatus.rc = 0; scanPageStatus.statusMessage = String.Format("File {0} scanned and data persisted to database", filename); } else if (scannedRecord.FailedMatches <= GetConstants.ScanErrorLimit()) { scanPageStatus.rc = 1; scanPageStatus.statusMessage = String.Format("Recorded {0} scan failures for source file {1}", scannedRecord.FailedMatches.ToString(), filename); } else { scanPageStatus.rc = -1; scanPageStatus.statusMessage = String.Format("Scan failures of {0} too high. No data saved for source file {1}", scannedRecord.FailedMatches.ToString(), filename); } if (scanPageStatus.rc >= 0) { // Persist data to database //var test = scannedRecord.ScannedFileName. scannedRecord.ScannedFileName = Path.GetFileName(filename); scannedRecord.ScanDate = DateTime.Now; // Will need to ensure this is correct time zone *** GPA db.ScannedRecords.InsertOnSubmit(scannedRecord); db.SubmitChanges(); } } //ss.Dispose(); } else { scanPageStatus.rc = 1; scanPageStatus.statusMessage = String.Format("Source file {0} null or empty", filename); } } catch (Exception ex) { scanPageStatus.rc = -1; scanPageStatus.statusMessage = String.Format("Exception {0} when running OCR on source file {1}", ex, filename); } return(scanPageStatus); }