private void Save(DateTime now, int i, List <ScannedImage> images) { if (images.Count == 0) { return; } var barcode = images[0].Barcode; var subPath = fileNamePlaceholders.SubstitutePlaceholders(Settings.SavePath, now, barcode, true, i); if (GetSavePathExtension().ToLower() == ".pdf") { if (File.Exists(subPath)) { subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, barcode, true, 0, 1); } var ocrLanguageCode = userConfigManager.Config.EnableOcr ? userConfigManager.Config.OcrLanguageCode : null; pdfExporter.Export(subPath, images, pdfSettingsContainer.PdfSettings, ocrLanguageCode, j => true); } else { var op = operationFactory.Create <SaveImagesOperation>(); op.Start(subPath, now, images, true); op.WaitUntilFinished(); } }
private async Task Save(DateTime now, int i, List <ScannedImage> images) { if (images.Count == 0) { return; } var subPath = fileNamePlaceholders.SubstitutePlaceholders(Settings.SavePath, now, true, i); if (GetSavePathExtension().ToLower() == ".pdf") { if (File.Exists(subPath)) { subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1); } var snapshots = images.Select(x => x.Preserve()).ToList(); try { await pdfExporter.Export(subPath, snapshots, pdfSettingsContainer.PdfSettings, ocrManager.DefaultParams, (j, k) => { }, CancelToken); } finally { snapshots.ForEach(s => s.Dispose()); } } else { var op = operationFactory.Create <SaveImagesOperation>(); op.Start(subPath, now, images, true); await op.Success; } }
public bool SavePdf(string fileName, DateTime dateTime, ICollection <IScannedImage> images, PdfSettings pdfSettings, string ocrLanguageCode, Func <int, bool> progressCallback) { var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime); if (File.Exists(subFileName)) { if (overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes) { return(false); } } try { pdfExporter.Export(subFileName, images, pdfSettings, ocrLanguageCode, progressCallback); return(true); } catch (UnauthorizedAccessException) { errorOutput.DisplayError(MiscResources.DontHavePermission); } catch (IOException ex) { Log.ErrorException(MiscResources.ErrorSaving, ex); errorOutput.DisplayError(MiscResources.ErrorSaving); } return(false); }
public bool Start(string fileName, DateTime dateTime, ICollection <ScannedImage> images, PdfSettings pdfSettings, string ocrLanguageCode, bool email) { ProgressTitle = email ? MiscResources.EmailPdfProgress : MiscResources.SavePdfProgress; var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime); Status = new OperationStatus { StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName)), MaxProgress = images.Count }; cancel = false; if (File.Exists(subFileName)) { if (overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes) { return(false); } } thread = threadFactory.StartThread(() => { try { Status.Success = pdfExporter.Export(subFileName, images, pdfSettings, ocrLanguageCode, i => { Status.CurrentProgress = i; InvokeStatusChanged(); return(!cancel); }); } catch (UnauthorizedAccessException) { InvokeError(MiscResources.DontHavePermission); } catch (Exception ex) { Log.ErrorException(MiscResources.ErrorSaving, ex); InvokeError(MiscResources.ErrorSaving); } GC.Collect(); InvokeFinished(); }); return(true); }
private void Save(DateTime now, int i, List <ScannedImage> images) { if (images.Count == 0) { return; } var subPath = fileNamePlaceholders.SubstitutePlaceholders(Settings.SavePath, now, true, i); if (GetSavePathExtension().ToLower() == ".pdf") { if (File.Exists(subPath)) { subPath = fileNamePlaceholders.SubstitutePlaceholders(subPath, now, true, 0, 1); } pdfExporter.Export(subPath, images, pdfSettingsContainer.PdfSettings, ocrDependencyManager.DefaultLanguageCode, j => true); } else { var op = operationFactory.Create <SaveImagesOperation>(); op.Start(subPath, now, images, true); op.WaitUntilFinished(); } }
public bool Start(string fileName, DateTime dateTime, ICollection <ScannedImage> images, PdfSettings pdfSettings, OcrParams ocrParams, bool email, EmailMessage emailMessage) { ProgressTitle = email ? MiscResources.EmailPdfProgress : MiscResources.SavePdfProgress; var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime); Status = new OperationStatus { StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName)), MaxProgress = images.Count }; if (Directory.Exists(subFileName)) { // Not supposed to be a directory, but ok... subFileName = fileNamePlaceholders.SubstitutePlaceholders(Path.Combine(subFileName, "$(n).pdf"), dateTime); } if (File.Exists(subFileName)) { if (overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes) { return(false); } } var snapshots = images.Select(x => x.Preserve()).ToList(); RunAsync(async() => { bool result = false; try { result = await pdfExporter.Export(subFileName, snapshots, pdfSettings, ocrParams, OnProgress, CancelToken); } catch (UnauthorizedAccessException ex) { InvokeError(MiscResources.DontHavePermission, ex); } catch (IOException ex) { if (File.Exists(subFileName)) { InvokeError(MiscResources.FileInUse, ex); } else { Log.ErrorException(MiscResources.ErrorSaving, ex); InvokeError(MiscResources.ErrorSaving, ex); } } catch (Exception ex) { Log.ErrorException(MiscResources.ErrorSaving, ex); InvokeError(MiscResources.ErrorSaving, ex); } finally { snapshots.ForEach(s => s.Dispose()); GC.Collect(); } if (result && email && emailMessage != null) { Status.StatusText = MiscResources.UploadingEmail; Status.CurrentProgress = 0; Status.MaxProgress = 1; Status.ProgressType = OperationProgressType.MB; InvokeStatusChanged(); try { result = await emailProviderFactory.Default.SendEmail(emailMessage, OnProgress, CancelToken); } catch (OperationCanceledException) { } catch (Exception ex) { Log.ErrorException(MiscResources.ErrorEmailing, ex); InvokeError(MiscResources.ErrorEmailing, ex); } } return(result); }); Success.ContinueWith(task => { if (task.Result) { if (email) { Log.Event(EventType.Email, new EventParams { Name = MiscResources.EmailPdf, Pages = snapshots.Count, FileFormat = ".pdf" }); } else { Log.Event(EventType.SavePdf, new EventParams { Name = MiscResources.SavePdf, Pages = snapshots.Count, FileFormat = ".pdf" }); } } }, TaskContinuationOptions.OnlyOnRanToCompletion); return(true); }
public void Export_Normal_CreatesFile() { Assert.IsFalse(File.Exists(PDF_PATH), "Error setting up test (test file not deleted)"); pdfExporter.Export(PDF_PATH, images, settings, null, num => true); Assert.IsTrue(File.Exists(PDF_PATH)); }
public bool Start(string fileName, DateTime dateTime, ICollection <ScannedImage> images, PdfSettings pdfSettings, OcrParams ocrParams, bool email, EmailMessage emailMessage) { ProgressTitle = email ? MiscResources.EmailPdfProgress : MiscResources.SavePdfProgress; Status = new OperationStatus { MaxProgress = images.Count }; if (Directory.Exists(fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime))) { // Not supposed to be a directory, but ok... fileName = Path.Combine(fileName, "$(n).pdf"); } var singleFile = !pdfSettings.SinglePagePdf || images.Count == 1; var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime); if (singleFile) { if (File.Exists(subFileName) && overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes) { return(false); } } var snapshots = images.Select(x => x.Preserve()).ToList(); var snapshotsByFile = pdfSettings.SinglePagePdf ? snapshots.Select(x => new[] { x }).ToArray() : new[] { snapshots.ToArray() }; RunAsync(async() => { bool result = false; try { int digits = (int)Math.Floor(Math.Log10(snapshots.Count)) + 1; int i = 0; foreach (var snapshotArray in snapshotsByFile) { subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, true, i, singleFile ? 0 : digits); Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName)); InvokeStatusChanged(); if (singleFile && IsFileInUse(subFileName, out var ex)) { InvokeError(MiscResources.FileInUse, ex); break; } var progress = singleFile ? OnProgress : (ProgressHandler)((j, k) => { }); result = await pdfExporter.Export(subFileName, snapshotArray, pdfSettings, ocrParams, progress, CancelToken); if (!result || CancelToken.IsCancellationRequested) { break; } emailMessage?.Attachments.Add(new EmailAttachment { FilePath = subFileName, AttachmentName = Path.GetFileName(subFileName) }); if (i == 0) { FirstFileSaved = subFileName; } i++; if (!singleFile) { OnProgress(i, snapshotsByFile.Length); } } } catch (UnauthorizedAccessException ex) { InvokeError(MiscResources.DontHavePermission, ex); } catch (IOException ex) { if (File.Exists(subFileName)) { InvokeError(MiscResources.FileInUse, ex); } else { Log.ErrorException(MiscResources.ErrorSaving, ex); InvokeError(MiscResources.ErrorSaving, ex); } } catch (Exception ex) { Log.ErrorException(MiscResources.ErrorSaving, ex); InvokeError(MiscResources.ErrorSaving, ex); } finally { snapshots.ForEach(s => s.Dispose()); GC.Collect(); } if (result && !CancelToken.IsCancellationRequested && email && emailMessage != null) { Status.StatusText = MiscResources.UploadingEmail; Status.CurrentProgress = 0; Status.MaxProgress = 1; Status.ProgressType = OperationProgressType.MB; InvokeStatusChanged(); try { result = await emailProviderFactory.Default.SendEmail(emailMessage, OnProgress, CancelToken); } catch (OperationCanceledException) { } catch (Exception ex) { Log.ErrorException(MiscResources.ErrorEmailing, ex); InvokeError(MiscResources.ErrorEmailing, ex); } } return(result); }); Success.ContinueWith(task => { if (task.Result) { if (email) { Log.Event(EventType.Email, new Event { Name = MiscResources.EmailPdf, Pages = snapshots.Count, FileFormat = ".pdf" }); } else { Log.Event(EventType.SavePdf, new Event { Name = MiscResources.SavePdf, Pages = snapshots.Count, FileFormat = ".pdf" }); if (pdfSettings.ShowFolder) { String filePath = Path.GetDirectoryName(fileName); Process.Start("explorer.exe", filePath); } } } }, TaskContinuationOptions.OnlyOnRanToCompletion); return(true); }