public static bool RemoveOcrRendition(this Record record) { bool removed = false; for (uint i = 0; i < record.ChildRenditions.Count; i++) { RecordRendition rendition = record.ChildRenditions.getItem(i) as RecordRendition; if ((RecordRendition)rendition != null && rendition.TypeOfRendition == RenditionType.Ocr) { Log.Debug($"Remove Rendition {rendition.Uri} from Record {record.Uri}"); rendition.Delete(); removed = true; } } if (removed) { record.Save(); Log.Info($"Removed Ocr rendition from record {record.Uri}"); } else { Log.Info($"No Ocr Rendition on record {record.Uri}"); } return(removed); }
public static void HandleEvent(Database db, TrimEvent evt) { Record record = null; RecordRendition rendition = null; try { record = db.FindTrimObjectByUri(BaseObjectTypes.Record, evt.ObjectUri) as Record; if (evt.RelatedObjectType == BaseObjectTypes.RecordRendition && evt.EventType == Events.DocRenditionAdded) { var eventRendition = record.ChildRenditions.FindChildByUri(evt.RelatedObjectUri) as RecordRendition; if (eventRendition != null && eventRendition.TypeOfRendition == RenditionType.Original) { // if added an original rendition = eventRendition; } } if (rendition != null) { RecordController.GenerateOcrRendition(record, rendition); } else { RecordController.UpdateOcrRendition(record); } } catch (TrimException ex) { Log.Error(ex); } finally { record = null; rendition = null; } }
public static bool HasOcrRendition(this Record record) { for (uint i = 0; i < record.ChildRenditions.Count; i++) { RecordRendition rendition = record.ChildRenditions.getItem(i) as RecordRendition; if ((RecordRendition)rendition != null && rendition.TypeOfRendition == RenditionType.Ocr) { return(true); } } return(false); }
public static bool GenerateOcrRendition(Record record, RecordRendition sourceRendition) { bool success = false; string extractedFilePath = string.Empty; string ocrFilePath = string.Empty; try { // get a temp working location on disk var rootDirectory = Path.Combine(Path.GetTempPath(), "cmramble_ocr"); if (!Directory.Exists(rootDirectory)) { Directory.CreateDirectory(rootDirectory); } // formulate file name to extract, delete if exists for some reason extractedFilePath = Path.Combine(rootDirectory, $"{sourceRendition.Uri}.{sourceRendition.Extension}"); ocrFilePath = Path.Combine(rootDirectory, $"{sourceRendition.Uri}.txt"); FileHelper.Delete(extractedFilePath); FileHelper.Delete(ocrFilePath); // fetch document var extract = sourceRendition.GetExtractDocument(); extract.FileName = Path.GetFileName(extractedFilePath); extract.DoExtract(Path.GetDirectoryName(extractedFilePath), true, false, ""); if (!String.IsNullOrWhiteSpace(extract.FileName) && File.Exists(extractedFilePath)) { ocrFilePath = TesseractOcr.ExtractFromFile(extractedFilePath); // use record extension method that removes existing OCR rendition (if exists) record.AddOcrRendition(ocrFilePath); record.Save(); success = true; } } catch (Exception ex) { } finally { FileHelper.Delete(extractedFilePath); FileHelper.Delete(ocrFilePath); } return(success); }