Exemple #1
0
        void buttonStart_Click(object sender, EventArgs e)
        {
            SplitSettings splitSettings = new SplitSettings();

            splitSettings.InputFile       = textBoxInputFile.Text;
            splitSettings.OutputDirectory = textBoxOutputDirectory.Text;

            if (radioButtonMulti.Checked == true)
            {
                splitSettings.MultiOutput = true;
                splitSettings.PageNumbers = new List <int>();

                if (numericUpDownPages.Value != 0)
                {
                    splitSettings.PageNumbers.Add(Convert.ToInt32(numericUpDownPages.Value));
                }
            }
            else
            {
                splitSettings.PageNumbers = splitSettings.GenerateNumberList(textBoxPages.Text);
            }

            List <string> errorMessage = Parameters.SearchForErrors(splitSettings);

            if (errorMessage.Count != 0)
            {
                MessageBox.Show(errorMessage[0], errorMessage[1]);
            }
            else
            {
                if (splitSettings.ChangedPageNumbers != null)
                {
                    MessageBox.Show($"The parameter for page numbers has been corrected: {Environment.NewLine}{splitSettings.ChangedPageNumbers}",
                                    "Parameter change");
                }

                try
                {
                    Splitter.SplitPDF(splitSettings);

                    if (checkBoxOpenResult.Checked)
                    {
                        if (splitSettings.MultiOutput)
                        {
                            System.Diagnostics.Process.Start(splitSettings.OutputDirectory);
                        }
                        else
                        {
                            System.Diagnostics.Process.Start(Path.Combine(splitSettings.OutputDirectory, splitSettings.PdfName));
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Exemple #2
0
        public static void SplitPDF(SplitSettings splitSettings)
        {
            if (splitSettings.MultiOutput == false)
            {
                PdfReader reader         = new PdfReader(splitSettings.InputFile);
                Document  document       = new Document(reader.GetPageSizeWithRotation(1));
                string    outputFullName = GetUniqueFileName(splitSettings.OutputDirectory, splitSettings.PdfName, ".pdf");
                splitSettings.PdfName = Path.GetFileName(outputFullName);
                PdfCopy copyProvider = new PdfCopy(document, new FileStream(outputFullName, FileMode.OpenOrCreate));
                document.Open();

                foreach (int pageNumber in splitSettings.PageNumbers)
                {
                    PdfImportedPage pdfPage = copyProvider.GetImportedPage(reader, pageNumber);
                    copyProvider.AddPage(pdfPage);
                }

                document.Close();
            }
            else
            {
                if (Directory.GetFiles(splitSettings.OutputDirectory, "*.pdf").Length != 0)
                {
                    splitSettings.OutputDirectory = CreateUniqueFolder(splitSettings.OutputDirectory, splitSettings.PdfName);
                }

                int       pagesNeeded = splitSettings.PageNumbers[0];
                PdfReader reader      = new PdfReader(splitSettings.InputFile);

                for (int i = 1; i <= splitSettings.PdfLastPage; i += pagesNeeded)
                {
                    string   outputFullName = $"{splitSettings.OutputDirectory}\\{splitSettings.PdfName}_{Convert.ToString((i - 1) / pagesNeeded + 1)}.pdf";
                    Document document       = new Document(reader.GetPageSizeWithRotation(1));
                    PdfCopy  copyProvider   = new PdfCopy(document, new FileStream(outputFullName, FileMode.OpenOrCreate));
                    document.Open();

                    for (int n = 0; n < pagesNeeded && i + n <= splitSettings.PdfLastPage; ++n)
                    {
                        PdfImportedPage pdfPage = copyProvider.GetImportedPage(reader, i + n);
                        copyProvider.AddPage(pdfPage);
                    }

                    document.Close();
                }
            }
        }
Exemple #3
0
        public static List <string> SearchForErrors(SplitSettings splitSettings)
        {
            List <string> errorMessage = new List <string>();

            if (splitSettings.InputFile == null)
            {
                if (splitSettings.FileExists)
                {
                    errorMessage.Add("The given file is not a valid PDF.");
                    errorMessage.Add("Wrong file");
                    errorMessage.Add("The given file (1st parameter) is not a valid PDF.");
                }
                else
                {
                    errorMessage.Add("The given file can't be found.");
                    errorMessage.Add("Wrong file");
                    errorMessage.Add("The given file (1st parameter) can't be found.");
                }
            }
            else if (splitSettings.OutputDirectory == null)
            {
                errorMessage.Add("The folder can't be reached or created.");
                errorMessage.Add("Invalid path");
                errorMessage.Add("The folder (2nd parameter) can't be reached or created.");
            }
            else if (splitSettings.PageNumbers.Count == 0)
            {
                if (splitSettings.MultiOutput)
                {
                    errorMessage.Add($"Please give a natural number {Environment.NewLine}to specify the amount of pages you need in one file.");
                    errorMessage.Add("Wrong page number");
                    errorMessage.Add("Please give a number larger than 0 to specify the amount of pages you need in one file.");
                }
                else
                {
                    errorMessage.Add($"Use numbers (mandatory), commas, hyphens or spaces {Environment.NewLine}to specify page numbers (e.g. \"1-5, 7\").");
                    errorMessage.Add("Wrong page numbers");
                    errorMessage.Add("Please use numbers (mandatory), commas, hyphens or spaces to specify page numbers (3rd parameter, e.g. \"1-5, 7\").");
                }
            }
            else if (splitSettings.PageNumbers[0] == -1)
            {
                errorMessage.Add("Please remove the redundant hyphen(s) from page numbers.");
                errorMessage.Add("Wrong page numbers");
                errorMessage.Add("Please remove the redundant hyphen(s) from page numbers (3rd parameter).");
            }
            else if (splitSettings.PageNumbers.Count > 1 && splitSettings.MultiOutput)
            {
                errorMessage.Add("Won't happen in GUI mode.");
                errorMessage.Add("");
                errorMessage.Add("If you include '!' to page numbers, you can only have one number (e.g. \"12!\")");
            }
            else if (splitSettings.PageNumbers[0] == splitSettings.PdfLastPage + 1)
            {
                errorMessage.Add($"The file has only {splitSettings.PdfLastPage} pages. Please give a smaller number.");
                errorMessage.Add("Page does not exist");
                errorMessage.Add($"The file has only {splitSettings.PdfLastPage} pages. Please give a smaller number (3rd parameter).");
            }

            return(errorMessage);
        }