protected override void Execute(NativeActivityContext context) { WorkflowDataContext dataContext = context.DataContext; PropertyDescriptorCollection propertyDescriptorCollection = dataContext.GetProperties(); string sessionId = string.Empty; int fileSystemItemId = 0; foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgSessionId") { sessionId = propertyDesc.GetValue(dataContext) as string; break; } } foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgFileSystemItemId") { fileSystemItemId = (int)propertyDesc.GetValue(dataContext); break; } } if (string.IsNullOrWhiteSpace(sessionId)) { LogHelper.AddGeneralLog(GeneralLogTypeEnum.ActivityError, $"SessionId is null."); return; } if (fileSystemItemId == 0) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"FileSystemItemId is null."); return; } FileSystemItemDto fileSystemItem = null; try { using (var sqlDbContext = new SqlDbContext()) { fileSystemItem = sqlDbContext.FileSystemItems.FirstOrDefault(x => x.Id == fileSystemItemId); } } catch (Exception ex) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"Exception has been thrown when getting fileSystemItem. FileSystemItemId: {fileSystemItemId}", ex); return; } Result.Set(context, fileSystemItem); }
protected override void Execute(NativeActivityContext context) { WorkflowDataContext dataContext = context.DataContext; PropertyDescriptorCollection propertyDescriptorCollection = dataContext.GetProperties(); string sessionId = string.Empty; int fileSystemItemId = 0; foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgSessionId") { sessionId = propertyDesc.GetValue(dataContext) as string; break; } } foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgFileSystemItemId") { fileSystemItemId = (int)propertyDesc.GetValue(dataContext); break; } } if (string.IsNullOrWhiteSpace(sessionId)) { LogHelper.AddGeneralLog(GeneralLogTypeEnum.ActivityError, $"SessionId is null."); return; } if (fileSystemItemId == 0) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"FileSystemItemId is null."); return; } FileSystemItemDto fileSystemItem = null; try { using (var sqlDbContext = new SqlDbContext()) { fileSystemItem = sqlDbContext.FileSystemItems.FirstOrDefault(x => x.Id == fileSystemItemId); } } catch (Exception ex) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"Exception has been thrown when getting fileSystemItem. FileSystemItemId: {fileSystemItemId}", ex); return; } var targetFile = TargetFilePathForTextFile.Get(context); int counter = 0; while (true) { counter++; if (File.Exists(targetFile)) { var directory = Path.GetDirectoryName(targetFile); var fileName = Path.GetFileNameWithoutExtension(targetFile); var extension = Path.GetExtension(targetFile); targetFile = Path.Combine(directory, fileName + counter + extension); } else { break; } } try { TempData.Instance.TempPath = Path.GetTempPath(); using (PDFDoc doc = PDFDoc.Open(fileSystemItem.FullPath)) { if (doc.GetText() == string.Empty) { doc.Ocr(OcrMode.Tesseract, "tur", WriteTextMode.Word); doc.Save(TargetFilePathForPDFFile.Get(context)); var ocrText = doc.GetText(); File.WriteAllText(TargetFilePathForTextFile.Get(context), ocrText); } } } catch (Exception ex) { LogHelper.AddFileSystemItemLog(fileSystemItemId, fileSystemItem.SourceId, sessionId, FileSystemItemLogTypeEnum.StoppedWithError, $"Exception has been thrown in OCR operation. FileSystemItemId: {fileSystemItemId}", ex); return; } LogHelper.AddFileSystemItemLog(fileSystemItem.Id, fileSystemItem.SourceId, sessionId, FileSystemItemLogTypeEnum.TesseractOcrOk); Result.Set(context, true); }
protected override void Execute(NativeActivityContext context) { WorkflowDataContext dataContext = context.DataContext; PropertyDescriptorCollection propertyDescriptorCollection = dataContext.GetProperties(); string sessionId = string.Empty; int fileSystemItemId = 0; foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgSessionId") { sessionId = propertyDesc.GetValue(dataContext) as string; break; } } foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgFileSystemItemId") { fileSystemItemId = (int)propertyDesc.GetValue(dataContext); break; } } if (string.IsNullOrWhiteSpace(sessionId)) { LogHelper.AddGeneralLog(GeneralLogTypeEnum.ActivityError, $"SessionId is null."); return; } if (fileSystemItemId == 0) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"FileSystemItemId is null."); return; } FileSystemItemDto fileSystemItem = null; try { using (var sqlDbContext = new SqlDbContext()) { fileSystemItem = sqlDbContext.FileSystemItems.FirstOrDefault(x => x.Id == fileSystemItemId); } } catch (Exception ex) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"Exception has been thrown when getting fileSystemItem. FileSystemItemId: {fileSystemItemId}", ex); return; } if (File.Exists(SourceFilePath.Get(context)) == false) { LogHelper.AddFileSystemItemLog(fileSystemItem.Id, fileSystemItem.SourceId, sessionId, FileSystemItemLogTypeEnum.SaveTextDataFileNotFound, $"File path: {SourceFilePath.Get(context)}."); return; } var content = File.ReadAllText(SourceFilePath.Get(context)); try { using (var sqlDbContext = new SqlDbContext()) { var saveResult1 = sqlDbContext.SaveChanges(); var oldItemCount = sqlDbContext.ClearTexts.Count(x => x.FileSystemItemId == fileSystemItemId); if (oldItemCount == 0) { var clearTextDto = new ClearTextDto { FileSystemItemId = fileSystemItemId, TextInFile = content, }; sqlDbContext.ClearTexts.Add(clearTextDto); var saveResult2 = sqlDbContext.SaveChanges(); } else { var oldClearText = sqlDbContext.ClearTexts.FirstOrDefault(x => x.FileSystemItemId == fileSystemItemId); var sql = "UPDATE ClearText SET TextInFile = @TextInFile WHERE Id = @Id"; var executeCount = sqlDbContext.Database.ExecuteSqlCommand(sql, content, oldClearText.Id); } } LogHelper.AddFileSystemItemLog(fileSystemItem.Id, fileSystemItem.SourceId, sessionId, FileSystemItemLogTypeEnum.SaveTextDataOk); } catch (Exception ex) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"Exception has been thrown when getting fileSystemItem. FileSystemItemId: {fileSystemItemId}", ex); return; } }
protected override void Execute(NativeActivityContext context) { WorkflowDataContext dataContext = context.DataContext; PropertyDescriptorCollection propertyDescriptorCollection = dataContext.GetProperties(); string sessionId = string.Empty; int fileSystemItemId = 0; foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgSessionId") { sessionId = propertyDesc.GetValue(dataContext) as string; break; } } foreach (PropertyDescriptor propertyDesc in propertyDescriptorCollection) { if (propertyDesc.Name == "ArgFileSystemItemId") { fileSystemItemId = (int)propertyDesc.GetValue(dataContext); break; } } if (string.IsNullOrWhiteSpace(sessionId)) { LogHelper.AddGeneralLog(GeneralLogTypeEnum.ActivityError, $"SessionId is null."); return; } if (fileSystemItemId == 0) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"FileSystemItemId is null."); return; } FileSystemItemDto fileSystemItem = null; try { using (var sqlDbContext = new SqlDbContext()) { fileSystemItem = sqlDbContext.FileSystemItems.FirstOrDefault(x => x.Id == fileSystemItemId); } } catch (Exception ex) { LogHelper.AddSessionLog(SessionLogTypeEnum.ActivityError, sessionId, $"Exception has been thrown when getting fileSystemItem. FileSystemItemId: {fileSystemItemId}", ex); return; } var targetFilePathForTextFile = TargetFilePathForTextFile.Get(context); var targetFilePathForPDFFile = TargetFilePathForPDFFile.Get(context); targetFilePathForTextFile = FileHelper.GetUnusedFileName(Path.GetDirectoryName(targetFilePathForTextFile), Path.GetFileName(targetFilePathForTextFile)); var dummyTiffFile = FileHelper.GetUnusedFileName(Path.GetTempPath(), Guid.NewGuid().ToString() + ".tiff"); // C:\Users\efeo\Downloads\ImageMagick-7.0.8-12-portable-Q16-x64\convert.exe -density 300 c:\AkisLog\A.pdf -depth 8 -strip -background white -alpha off c:\AkisLog\a.tiff try { Process process = new Process(); ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:\Users\efeo\Downloads\ImageMagick-7.0.8-12-portable-Q16-x64\convert.exe", Arguments = $"-density 300 {fileSystemItem.FullPath} -depth 8 -strip -background white -alpha off {dummyTiffFile}" }; process.StartInfo = processStartInfo; try { process.Start(); process.WaitForExit(); } catch (Exception ex) { logger.Error(ex, $"Process error convert."); return; } } catch (Exception ex) { LogHelper.AddFileSystemItemLog(fileSystemItemId, fileSystemItem.SourceId, sessionId, FileSystemItemLogTypeEnum.StoppedWithError, $"Exception has been thrown in OCR operation. FileSystemItemId: {fileSystemItemId}", ex); return; } var canOsman = false; if (File.Exists(dummyTiffFile)) { try { var fileInfo = new FileInfo(dummyTiffFile); if (fileInfo.Length > 0) { canOsman = true; } } catch (Exception ex) { logger.Error(ex, $"Check file error."); return; } } if (canOsman == false) { } var tessData = ""; // "C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" "c:\AkisLog\c.tiff" "c:\AkisLog\c" --tessdata-dir "C:\Program Files (x86)\Tesseract-OCR\tessdata" -l tur pdf try { Process process = new Process(); ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe", Arguments = $"\"{dummyTiffFile}\" \"{targetFilePathForPDFFile}\" --tessdata-dir \"{tessData}\" -l tur pdf" }; process.StartInfo = processStartInfo; try { process.Start(); process.WaitForExit(); } catch (Exception ex) { logger.Error(ex, $"Process error tesseract."); return; } } catch (Exception ex) { LogHelper.AddFileSystemItemLog(fileSystemItemId, fileSystemItem.SourceId, sessionId, FileSystemItemLogTypeEnum.StoppedWithError, $"Exception has been thrown in OCR operation. FileSystemItemId: {fileSystemItemId}", ex); return; } LogHelper.AddFileSystemItemLog(fileSystemItem.Id, fileSystemItem.SourceId, sessionId, FileSystemItemLogTypeEnum.TesseractOcrOk); Result.Set(context, true); }