Beispiel #1
0
        private void buttonSplit_Click(object sender, EventArgs 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.Checked;

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

                if ((this.radioButtonPages.Checked && regexNums.IsMatch(args.FromPage) && (args.ToPage.Length > 0? regexNums.IsMatch(args.ToPage) : true)) || (this.radioButtonFiles.Checked && regexNums.IsMatch(args.NumOfPages)))
                {
                    this.args = args;
                }
                else
                {
                    MessageBox.Show(this, "Input invalid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.DialogResult = DialogResult.None;       
                }
            }
            else
            {
                MessageBox.Show(this, "Input incomplete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
            }
        }
Beispiel #2
0
        private void buttonSplit_Click(object sender, EventArgs 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.Checked;

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

                if ((this.radioButtonPages.Checked && regexNums.IsMatch(args.FromPage) && (args.ToPage.Length > 0? regexNums.IsMatch(args.ToPage) : true)) || (this.radioButtonFiles.Checked && regexNums.IsMatch(args.NumOfPages)))
                {
                    this.args = args;
                }
                else
                {
                    MessageBox.Show(this, "Input invalid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.DialogResult = DialogResult.None;
                }
            }
            else
            {
                MessageBox.Show(this, "Input incomplete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
            }
        }
Beispiel #3
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;
        }