private void buttonSplit_Click(object sender, RoutedEventArgs e)
        {
            SplitPdfArgs args = new SplitPdfArgs();

            args.InputFilename  = this.textBoxInput.Text;
            args.OutputFilename = this.textBoxOutput.Text;
            args.FromPage       = this.textBoxFrom.Text;
            args.ToPage         = this.textBoxTo.Text;
            args.NumOfPages     = this.textBoxNumOfPages.Text;
            args.Pages          = this.radioButtonPages.IsChecked.Value;

            if (args.InputFilename.Length > 0 && args.OutputFilename.Length > 0 &&
                ((this.radioButtonPages.IsChecked.Value && args.FromPage.Length > 0) ||
                 (this.radioButtonFiles.IsChecked.Value && args.NumOfPages.Length > 0)))
            {
                Regex regexNums = new Regex(@"^\d+$");

                if ((this.radioButtonPages.IsChecked.Value && regexNums.IsMatch(args.FromPage) &&
                     (args.ToPage.Length > 0 ? regexNums.IsMatch(args.ToPage) : true)) ||
                    (this.radioButtonFiles.IsChecked.Value && regexNums.IsMatch(args.NumOfPages)))
                {
                    this.args         = args;
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show(this, "Input invalid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show(this, "Input incomplete.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 2
0
        private void backgroundWorkerSplitPdf_DoWork(object sender, DoWorkEventArgs e)
        {
            SplitPdfArgs args = (SplitPdfArgs)e.Argument;

            if (args.Pages)
            {
                PdfUtilities.SplitPdf(args.InputFilename, args.OutputFilename, args.FromPage, args.ToPage);
            }
            else
            {
                string outputFilename = String.Empty;

                if (args.OutputFilename.EndsWith(".pdf"))
                {
                    outputFilename = args.OutputFilename.Substring(0, args.OutputFilename.LastIndexOf(".pdf"));
                }

                int pageCount = PdfUtilities.GetPdfPageCount(args.InputFilename);
                if (pageCount == 0)
                {
                    throw new ApplicationException("Split PDF failed.");
                }

                int pageRange = Int32.Parse(args.NumOfPages);
                int startPage = 1;

                while (startPage <= pageCount)
                {
                    int    endPage    = startPage + pageRange - 1;
                    string outputFile = outputFilename + startPage + ".pdf";
                    PdfUtilities.SplitPdf(args.InputFilename, outputFile, startPage.ToString(), endPage.ToString());
                    startPage = endPage + 1;
                }
            }

            e.Result = args.OutputFilename;
        }