public async void ShowScanAndAdd()
        {
            _model.IsMsgVisible = true;
            var scanner = new ScannerService();
            var pAsync  = await activeWindow.ShowProgressAsync("Status", "Starting...", false, s_without_animation);

            pAsync.SetIndeterminate();

            var converter = new ScannerImageConverter(_model.TempFolder);

            switch (_model.AutoAdd)
            {
            case true:
                await Task.Factory.StartNew(async() =>
                {
                    try
                    {
                        var filee = scanner.ScanAll(pAsync, _model.ColorSetting);
                        if (filee.Count == 0)
                        {
                            await pAsync.CloseAsync();
                            return;
                        }

                        converter.SavePDFsOn(filee, pAsync);
                        await pAsync.CloseAsync();
                        _model.IsMsgVisible = false;
                        AcrobatProcess();
                        await Task.Delay(500);

                        pAsync.SetMessage("Adding pages to file...");
                        await Task.Factory.StartNew(() => converter.AddToExistingPDF($@"{_model.TempFolder}Preview.pdf", $@"{_model.TempFolder}{_model.FileNameAdded}.pdf"));
                        App.Current.Dispatcher.Invoke(() => {
                            activeWindow.FindChild <WebBrowser>("pdfview").Navigate($@"{_model.TempFolder}{_model.FileNameAdded}.pdf");
                        });
                        await pAsync.CloseAsync();
                        _model.IsMsgVisible = false;
                    }
                    catch (Exception ex)
                    {
                        await pAsync.CloseAsync();


                        App.Current.Dispatcher.Invoke(() => {
                            ShowErrorMessage(ex.Message);
                        });
                    }
                });

                break;

            case false:
                var file = await Task <ImageFile> .Factory.StartNew(() => scanner.Scan(pAsync, _model.ColorSetting)).ContinueWith(async(t) =>
                {
                    try
                    {
                        if (t != null)
                        {
                            var scannedImage = converter.ConvertScannedImage(t.Result);
                            var saveResult   = converter.SaveOnPDF(scannedImage as BitmapFrame, pAsync);
                            AcrobatProcess();
                            await Task.Delay(500);

                            pAsync.SetMessage("Adding pages to file...");
                            await Task.Factory.StartNew(() => converter.AddToExistingPDF($@"{_model.TempFolder}Preview.pdf", $@"{_model.TempFolder}{_model.FileNameAdded}.pdf"));
                            App.Current.Dispatcher.Invoke(() => {
                                activeWindow.FindChild <WebBrowser>("pdfview").Navigate($@"{_model.TempFolder}{_model.FileNameAdded}.pdf");
                            });

                            await pAsync.CloseAsync();
                            _model.IsMsgVisible = false;
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        await pAsync.CloseAsync();

                        App.Current.Dispatcher.Invoke(() => {
                            ShowErrorMessage(ex.Message);
                        });
                    }
                });

                break;


            default:
                break;
            }

            // _model.IsMsgVisible = false;
        }
Ejemplo n.º 2
0
        public async void ShowScanAll()
        {
            _model.VisibilityHeader = Visibility.Hidden;
            var pAsync = await activeWindow.ShowProgressAsync("Status", "Scanning...", false, s_without_animation);

            pAsync.SetIndeterminate();
            await Task.Delay(500);

            if (File.Exists($"{_model.TempFolder}Preview.pdf"))
            {
                activeWindow.FindChild <WebBrowser>("pdfview").Navigate("about:blank");
                AcrobatProcess();
                await Task.Delay(500);

                File.Delete($"{_model.TempFolder}Preview.pdf");
            }

            var scanner   = new ScannerService();
            var converter = new ScannerImageConverter(_model.TempFolder);

            await Task.Factory.StartNew(async() =>
            {
                try
                {
                    var file = scanner.ScanAll(pAsync, _model.ColorSetting);
                    if (file.Count == 0)
                    {
                        await pAsync.CloseAsync();
                        return;
                    }

                    _model.ScannedImage     = null;
                    _model.OpenedFileName   = "Preview";
                    _model.FileNameToSave   = _model.OpenedFileName;
                    _model.OpenedFileFolder = _model.TempFolder;
                    converter.SavePDFsOn(file, pAsync);
                    if (File.Exists($@"{_model.TempFolder}{_model.OpenedFileName}.pdf"))
                    {
                        _model.FullPathOpenedFile = $@"{_model.TempFolder}{_model.OpenedFileName}";
                    }
                    else
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    ControlsErrorState();
                    await pAsync.CloseAsync();
                    ShowErrorMessage(message: ex.Message);
                    _model.ScannedImage = null;
                }
            });

            if (File.Exists($@"{_model.FullPathOpenedFile}.pdf"))
            {
                activeWindow.FindChild <WebBrowser>("pdfview").Navigate($"{_model.FullPathOpenedFile}.pdf");
                _model.SaveAsEnable = true;
            }
            else
            {
                return;
            }

            await pAsync.CloseAsync();

            _model.VisibilityHeader = Visibility.Visible;
        }