Example #1
0
 public static void Main(string[] args)
 {
     Options commandLineOptions = new Options();
     ICommandLineParser commandParser = new CommandLineParser();
     if (commandParser.ParseArguments(args, commandLineOptions, Console.Error))
     {
         if (ValidateOptions(commandLineOptions))
         {
             try
             {
                 TaskProcessor concatTask = new TaskProcessor();
                 concatTask.ProcessTask(commandLineOptions);
             }
             catch (Exception ex)
             {
                 StringBuilder errorMessage = new StringBuilder();
                 errorMessage.AppendLine(messageUnexpectedError);
                 if (commandLineOptions.DebugMessages == true)
                 {
                     errorMessage.AppendFormat(messageUnhandledException, ex.ToString(), ex.Message, ex.StackTrace);
                 }
                 System.Console.Error.WriteLine(errorMessage.ToString());
                 Environment.ExitCode = 1;
             }
         }
     }
     else
     {
         // Command line params could not be parsed,
         // or help was requested
         Environment.ExitCode = -1;
     }
 }
Example #2
0
 private static bool ValidateOptions(Options commandLineOptions)
 {
     bool validatedOK = false;
     StringBuilder errorMessage = new StringBuilder();
     if (commandLineOptions.Items.Count > 0)
     {
         // Make sure user has specified pages to extract
         if (commandLineOptions.ExtractPages != null && commandLineOptions.ExtractPages.Count > 0)
         {
             // Make sure the input file can actually be
             // opened
             try
             {
                 using (FileStream inputFile = new FileStream(commandLineOptions.Items[0], FileMode.Open, FileAccess.Read))
                 {
                     inputFile.Close();
                 }
             }
             catch
             {
                 errorMessage.AppendLine(String.Format(messageFileNotFound, commandLineOptions.Items[0]));
             }
             if (errorMessage.Length == 0)
             {
                 // Validate the extract page parameters
                 Regex singlePage = new Regex(@"^\d+$", RegexOptions.IgnorePatternWhitespace);
                 Regex pageRange = new Regex(@"^\d+-\d+$", RegexOptions.IgnorePatternWhitespace);
                 foreach (String extractPageParameter in commandLineOptions.ExtractPages)
                 {
                     if (!singlePage.IsMatch(extractPageParameter))
                     {
                         if (!pageRange.IsMatch(extractPageParameter))
                         {
                             // Parameter is neither a valid page
                             // nor a valid page range
                             errorMessage.AppendLine(String.Format(messageInvalidExtractPageOrRange, extractPageParameter));
                             break;
                         }
                         else
                         {
                             // Valid range format
                             // Make sure the start page in the range
                             // is less than the end page, and that
                             // neither page is zero
                             String[] extractPages = extractPageParameter.Split('-');
                             int startPage, endPage;
                             if (!(Int32.TryParse(extractPages[0], out startPage) && Int32.TryParse(extractPages[1], out endPage)
                                 && (endPage >= startPage) && startPage >= 1 && endPage >= 1))
                             {
                                 errorMessage.AppendLine(String.Format(messageInvalidExtractRange, extractPageParameter));
                                 break;
                             }
                         }
                     }
                     else
                     {
                         // Make sure single page is not zero
                         int extractPage;
                         if (!(Int32.TryParse(extractPageParameter, out extractPage) &&
                             extractPage >= 1))
                         {
                             errorMessage.AppendLine(String.Format(messageInvalidExtractPageOrRange, extractPageParameter));
                             break;
                         }
                     }
                 }
             }
             if (String.IsNullOrEmpty(errorMessage.ToString())) validatedOK = true;
         }
         else
         {
             // No extract pages specified
             errorMessage.Append(messageNoExtractPagesSpecifed);
         }
     }
     else
     {
         // No input file specified
         errorMessage.Append(messageNoInputFileSpecifed);
     }
     if (!validatedOK) System.Console.Error.WriteLine(errorMessage.ToString());
     return validatedOK;
 }
Example #3
0
        public void ProcessTask(Options commandLineOptions)
        {
            var pdfTools = new CoreTools();
            try
            {
                Regex singlePage = new Regex(@"^\d+$", RegexOptions.IgnorePatternWhitespace);
                Regex pageRange = new Regex(@"^\d+-\d+$", RegexOptions.IgnorePatternWhitespace);
                String outputPrefix;
                if (!String.IsNullOrEmpty(commandLineOptions.OutputFilePrefix))
                {
                    outputPrefix = commandLineOptions.OutputFilePrefix;
                }
                else
                {
                    outputPrefix = Path.GetFileNameWithoutExtension(commandLineOptions.Items[0]);
                }
                int[] extractPages = { 0, 0 };
                for (int loop = 0; loop < commandLineOptions.ExtractPages.Count; loop++)
                {
                    if (pageRange.IsMatch(commandLineOptions.ExtractPages[loop]))
                    {
                        String[] extractRange = commandLineOptions.ExtractPages[loop].Split('-');
                        extractPages[0] = Convert.ToInt32(extractRange[0]);
                        extractPages[1] = Convert.ToInt32(extractRange[1]);
                    }
                    else
                    {
                        extractPages[0] = Convert.ToInt32(commandLineOptions.ExtractPages[loop]);
                        extractPages[1] = Convert.ToInt32(commandLineOptions.ExtractPages[loop]);
                    }
                    pdfTools.ExtractPDFPages(commandLineOptions.Items[0],
                                             outputPrefix + "_" + (loop + 1).ToString() + ".PDF",
                                             extractPages[0],
                                             extractPages[1]);
                }
            }
            catch (System.IO.IOException ioException)
            {
                // PDF file is not valid, or was not found
                if (ioException.Message.Contains("PDF"))
                {
                    System.Console.Error.WriteLine(Environment.NewLine + "Input file is not a valid PDF.");
                }
                else if (ioException.Message.Contains("not found as file or resource"))
                {
                    System.Console.Error.WriteLine(Environment.NewLine + ioException.Message);
                }
                else
                {
                    throw;
                }
            }
            catch (ArgumentOutOfRangeException argException)
            {
                if (argException.Message.Contains("the number of pages in the document"))
                {
                    System.Console.Error.WriteLine("A page after the last page was specified.");
                }
                else if (argException.Message.Contains("Parameter cannot be zero or negative"))
                {
                    System.Console.Error.WriteLine("A page number less than one was specified.");
                }

            }
            catch (UnauthorizedAccessException)
            {
                System.Console.Error.WriteLine("Access denied.");
            }
        }
Example #4
0
        private static bool ValidateOptions(Options commandLineOptions)
        {
            bool          validatedOK  = false;
            StringBuilder errorMessage = new StringBuilder();

            if (commandLineOptions.Items.Count > 0)
            {
                // Make sure user has specified pages to extract
                if (commandLineOptions.ExtractPages != null && commandLineOptions.ExtractPages.Count > 0)
                {
                    // Make sure the input file can actually be
                    // opened
                    try
                    {
                        using (FileStream inputFile = new FileStream(commandLineOptions.Items[0], FileMode.Open, FileAccess.Read))
                        {
                            inputFile.Close();
                        }
                    }
                    catch
                    {
                        errorMessage.AppendLine(String.Format(messageFileNotFound, commandLineOptions.Items[0]));
                    }
                    if (errorMessage.Length == 0)
                    {
                        // Validate the extract page parameters
                        Regex singlePage = new Regex(@"^\d+$", RegexOptions.IgnorePatternWhitespace);
                        Regex pageRange  = new Regex(@"^\d+-\d+$", RegexOptions.IgnorePatternWhitespace);
                        foreach (String extractPageParameter in commandLineOptions.ExtractPages)
                        {
                            if (!singlePage.IsMatch(extractPageParameter))
                            {
                                if (!pageRange.IsMatch(extractPageParameter))
                                {
                                    // Parameter is neither a valid page
                                    // nor a valid page range
                                    errorMessage.AppendLine(String.Format(messageInvalidExtractPageOrRange, extractPageParameter));
                                    break;
                                }
                                else
                                {
                                    // Valid range format
                                    // Make sure the start page in the range
                                    // is less than the end page, and that
                                    // neither page is zero
                                    String[] extractPages = extractPageParameter.Split('-');
                                    int      startPage, endPage;
                                    if (!(Int32.TryParse(extractPages[0], out startPage) && Int32.TryParse(extractPages[1], out endPage) &&
                                          (endPage >= startPage) && startPage >= 1 && endPage >= 1))
                                    {
                                        errorMessage.AppendLine(String.Format(messageInvalidExtractRange, extractPageParameter));
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                // Make sure single page is not zero
                                int extractPage;
                                if (!(Int32.TryParse(extractPageParameter, out extractPage) &&
                                      extractPage >= 1))
                                {
                                    errorMessage.AppendLine(String.Format(messageInvalidExtractPageOrRange, extractPageParameter));
                                    break;
                                }
                            }
                        }
                    }
                    if (String.IsNullOrEmpty(errorMessage.ToString()))
                    {
                        validatedOK = true;
                    }
                }
                else
                {
                    // No extract pages specified
                    errorMessage.Append(messageNoExtractPagesSpecifed);
                }
            }
            else
            {
                // No input file specified
                errorMessage.Append(messageNoInputFileSpecifed);
            }
            if (!validatedOK)
            {
                System.Console.Error.WriteLine(errorMessage.ToString());
            }
            return(validatedOK);
        }