Example #1
0
        private void OnSplitPdfCmdExecute()
        {
            string          destinationPath = string.Empty;
            PageRangeParser pageRangeParser = null;

            IsBusy = true;
            bool isValid = ViewIsValid(out pageRangeParser);

            if (isValid)
            {
                if (SaveType == SaveOptions.UseSourceFolder)
                {
                    destinationPath = string.Format("{0}\\{1}",
                                                    PdfFiles.First().Info.DirectoryName, BaseFileName);
                }
                else if (SaveType == SaveOptions.UseCustomFolder)
                {
                    destinationPath = string.Format("{0}\\{1}",
                                                    DestinationFolder, BaseFileName);
                }

                splitBackgroundWorker.RunWorkerAsync(new object[] { PdfFiles.First(),
                                                                    pageRangeParser, destinationPath, OverwriteFile });
            }
            else
            {
                IsBusy = false;
            }
        }
        private void LoadPdfWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            string result = string.Empty;

            if (e.Error != null)
            {
                result = string.Format("An unexpected error occured while loading PDF file: {0}.", e.Error.Message);
            }
            else
            {
                object[] args = (object[])e.Result;
                if (string.IsNullOrEmpty(args[0].ToString()))
                {
                    PdfFiles.Add(args[1] as PdfFile);
                    SelectedFile = PdfFiles.First();
                }
                else
                {
                    result = args[0].ToString();
                }
            }

            Status = result;
            IsBusy = false;
        }
Example #3
0
        private void OnOpenFileCmdExecute()
        {
            WebBrowserView      webBrowserView      = new WebBrowserView();
            WebBrowserViewModel webBrowserViewModel = new WebBrowserViewModel(PdfFiles.First().Info.FullName);

            webBrowserView.DataContext = webBrowserViewModel;
            webBrowserView.Owner       = App.Current.MainWindow;
            webBrowserView.Show();
        }
Example #4
0
        public void RemoveSelectedFile()
        {
            if (SelectedFile == null)
            {
                System.Windows.MessageBox.Show("Please select a file to remove.", "No File Selected");
                return;
            }
            var pdf = PdfFiles.First(c => c == SelectedFile.Path);

            PdfFiles.Remove(pdf);
            PdfFilesList.Remove(SelectedFile);
            SelectedFile = null;
        }
Example #5
0
        private void OnUnlockCmdExecute()
        {
            PdfLoginView pdfLoginView = new PdfLoginView();

            pdfLoginView.DataContext = new PdfLoginViewModel(pdfLoginView, PdfFiles.First());
            pdfLoginView.Owner       = App.Current.MainWindow;
            bool?dialogResult = pdfLoginView.ShowDialog();

            if (dialogResult.HasValue && dialogResult.Value)
            {
                ProgressStatus = "PDF file successfully unlocked.";
            }
            else
            {
                ProgressStatus = "Failed to unlock PDF file.";
            }
        }
Example #6
0
        private void FileWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            string fileName = e.Argument.ToString();

            PdfFile pdfFile = new PdfFile(fileName);
            int     status  = pdfFile.Open();

            if (status == Define.Success ||
                status == Define.BadPasswordException)
            {
                App.Current.Dispatcher.Invoke(delegate()
                {
                    PdfFiles.Add(pdfFile);
                    SelectedFile = PdfFiles.First();
                });
            }
            else
            {
                e.Result = pdfFile.ErrorMsg;
            }
        }
Example #7
0
        private bool ViewIsValid(out PageRangeParser pageRangeParser)
        {
            bool   isValid  = false;
            string errorMsg = string.Empty;

            pageRangeParser = null;

            ClearViewErrors();
            if (PdfFiles.Count == 0)
            {
                ProgressStatus = "Please select a PDF file to split using the + button or [ Ins ].";
            }
            else if (PdfFiles.First().IsLocked)
            {
                ProgressStatus = "Please unlock the PDF file by clicking the lock button in the list or [ Ctrl+U ].";
            }
            else
            {
                if (FileHelpers.FileNameIsValid(BaseFileName, out errorMsg) != Define.Success)
                {
                    SetErrors("BaseFileName", new List <ValidationResult>()
                    {
                        new ValidationResult(false, errorMsg)
                    });
                }

                if (SaveType == SaveOptions.UseCustomFolder &&
                    FileHelpers.FolderIsValid(DestinationFolder, out errorMsg) != Define.Success)
                {
                    SetErrors("DestinationFolder", new List <ValidationResult>()
                    {
                        new ValidationResult(false, errorMsg)
                    });
                }

                errorMsg = string.Empty;
                if (SplitMethod == DocSplitMethod.Interval)
                {
                    if (string.IsNullOrEmpty(PageInterval))
                    {
                        errorMsg = "Split page interval is required.";
                    }
                    else
                    {
                        pageRangeParser = new PageRangeParser(PdfFiles.First().Reader.NumberOfPages, int.Parse(PageInterval));
                        if (pageRangeParser.Validate() != Define.Success)
                        {
                            errorMsg = pageRangeParser.ErrorMsg;
                        }
                    }

                    if (errorMsg != string.Empty)
                    {
                        SetErrors("PageInterval", new List <ValidationResult>()
                        {
                            new ValidationResult(false, errorMsg)
                        });
                    }
                }
                else if (SplitMethod == DocSplitMethod.Range)
                {
                    if (string.IsNullOrEmpty(PageRange))
                    {
                        errorMsg = "Split page range is required.";
                    }
                    else
                    {
                        pageRangeParser = new PageRangeParser(PdfFiles.First().Reader.NumberOfPages, PageRange);
                        if (pageRangeParser.Validate() != Define.Success)
                        {
                            errorMsg = pageRangeParser.ErrorMsg;
                        }
                    }

                    if (errorMsg != string.Empty)
                    {
                        SetErrors("PageRange", new List <ValidationResult>()
                        {
                            new ValidationResult(false, errorMsg)
                        });
                    }
                }

                isValid = !HasErrors;
            }
            return(isValid);
        }
Example #8
0
        public Task ProcessSelectedDocumentsAsync(IProgress <TaskProgress> prog, CancellationToken token, List <DocumentTypes> types, EtaCalculator pc, List <FileInfo> files)
        {
            var currentFile = 0.0;
            var fileCount   = Convert.ToDouble(files.Count);

            return(Task.Run(() =>
            {
                foreach (var file in files)
                {
                    var criteriaMatches = types.Select(o => new CriteriaMatchModel {
                        DocumentType = o
                    }).ToList();
                    using (var observedImage = CvInvoke.Imread(file.FullName))
                    {
                        Parallel.ForEach(_criteriaImages, (criteriaImage) =>
                        {
                            var criteriaFile = criteriaImage.Info;
                            var criteriaFileSplit = criteriaFile.Name.Split('-');
                            var type = types.First(c => c.DocumentType == criteriaFileSplit[0]);
                            var score = Classify(criteriaImage, observedImage);
                            var existingModel = criteriaMatches.First(c => c.DocumentType == type);
                            existingModel.Score += score;
                            existingModel.PdfFile = file.FullName;
                        });
                    }
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    var matchedCriteria = criteriaMatches.First(c => c.Score == criteriaMatches.Max(p => p.Score));
                    Console.WriteLine($"Score: {matchedCriteria.Score}");
                    if (matchedCriteria.Score >= matchedCriteria.DocumentType.AverageScore)
                    {
                        DocumentSelectionList.First(c => c.DocumentType == matchedCriteria.DocumentType.DocumentType).Matches += 1;
                        System.Windows.Application.Current.Dispatcher.Invoke(() =>
                        {
                            SelectionViewSource.Refresh();
                        });
                        var matchedFileName = file.Name.Substring(0, file.Name.Length - 4).Split('.')[0];
                        var matchedFile = PdfFiles.First(c => c.Contains(matchedFileName));
                        var matchedFileExtension = matchedFile.Substring(matchedFile.Length - 3);
                        if (matchedFileExtension.Equals("pdf", StringComparison.CurrentCultureIgnoreCase))
                        {
                            ExtractPageFromPdf(file, matchedCriteria.DocumentType.DocumentType, NamingModels);
                        }
                        else
                        {
                            CreatePdfFromImage(file, matchedCriteria.DocumentType.DocumentType, NamingModels);
                        }
                    }
                    currentFile++;
                    var rawProgress = (currentFile / fileCount);
                    var progress = rawProgress * 100;
                    var progressFloat = (float)rawProgress;
                    pc.Update(progressFloat);
                    if (pc.ETAIsAvailable)
                    {
                        var timeRemaining = pc.ETR.ToString(@"dd\.hh\:mm\:ss");
                        prog.Report(new TaskProgress
                        {
                            ProgressText = file.Name,
                            ProgressPercentage = progress,
                            ProgressText2 = timeRemaining
                        });
                    }
                    else
                    {
                        prog.Report(new TaskProgress
                        {
                            ProgressText = file.Name,
                            ProgressPercentage = progress,
                            ProgressText2 = "Calculating..."
                        });
                    }
                }
            }));
        }