Beispiel #1
0
 public static void Main(string[] args)
 {
     Options commandLineOptions = new Options();
     ICommandLineParser commandParser = new CommandLineParser(new CommandLineParserSettings(true, true));
     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)
                 {
                     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;
     }
 }
Beispiel #2
0
        private static bool ValidateOptions(Options commandLineOptions)
        {
            bool validatedOK = true;
            String errorMessage = null;

            if (commandLineOptions.Items.Count > 0)
            {
                if (!commandLineOptions.allPages) // No need to check -s options since -a was specified
                {
                    if (commandLineOptions.SplitPages != null && commandLineOptions.SplitPages.Count > 0)
                    {
                        foreach (String splitPage in commandLineOptions.SplitPages)
                        {
                            UInt32 parseResult;
                            if (!UInt32.TryParse(splitPage, out parseResult))
                            {
                                errorMessage = String.Format(messageInvalidSplitPage, splitPage);
                                break;
                            }
                        }

                    }
                    else
                    {
                        errorMessage = messageNoSplitPagesSpecifed;
                    }
                }
            }
            else
            {
                errorMessage = messageNoInputFileSpecifed;
            }
            if (!String.IsNullOrEmpty(errorMessage))
            {
                validatedOK = false;
                Console.Error.WriteLine(Environment.NewLine + errorMessage);
                Environment.ExitCode = 1;
            }
            return validatedOK;
        }
Beispiel #3
0
        public void ProcessTask(Options commandLineOptions)
        {
            String inputFile = commandLineOptions.Items[0];
            var splitTools = new CoreTools();
            List<String> splitPages;
            switch (commandLineOptions.allPages)
            {
                case (true):
                    // Splitting out every single page,
                    // so there has to be any entry for
                    // every page in the List object
                    Dictionary<String, String> pdfInfo = splitTools.RetrieveBasicProperties(inputFile);
                    int pageCount = Convert.ToInt32(pdfInfo["Page Count"]);
                    splitPages = new List<string>();
                    for (int loop = 2; loop <= pageCount; loop++)
                    {
                        splitPages.Add(loop.ToString());
                    }
                    break;
                default:
                    splitPages = commandLineOptions.SplitPages.Distinct().ToList<String>();
                    splitPages.Sort();
                    break;
            }
            String outputFilePrefix;
            if (!String.IsNullOrWhiteSpace(commandLineOptions.OutputFilePrefix))
            {
                outputFilePrefix = commandLineOptions.OutputFilePrefix;
            }
            else
            {
                if (!String.IsNullOrEmpty(Path.GetDirectoryName(commandLineOptions.Items[0])))
                {
                    outputFilePrefix = Path.Combine(Path.GetDirectoryName(commandLineOptions.Items[0]),Path.GetFileNameWithoutExtension(commandLineOptions.Items[0]));
                }
                else
                {
                    outputFilePrefix = Path.GetFileNameWithoutExtension(commandLineOptions.Items[0]);
                }
            }
            outputFilePrefix += "{0:" + new String('0', splitPages.Count.ToString().Length) + "}.PDF";
            var splitStartPages = new SortedList<int, String>();

            for (int loop = 0; loop < splitPages.Count; loop++)
            {
                splitStartPages.Add(Convert.ToInt32(splitPages[loop]), String.Format(outputFilePrefix, loop + 2));
            }
            if (!splitStartPages.ContainsKey(1)) splitStartPages.Add(1, String.Format(outputFilePrefix, 1)); // Add page 1 if not specified by user (it usually isn't)
            try
            {
                splitTools.SplitPDF(inputFile, splitStartPages);
            }
            catch (ArgumentOutOfRangeException outOfRangeException)
            {
                // Page 0 or page number greater than # of pages in PDF specified
                String consoleMessage = outOfRangeException.Message.Remove(outOfRangeException.Message.LastIndexOf("Parameter name:", StringComparison.CurrentCultureIgnoreCase));
                System.Console.Error.WriteLine(Environment.NewLine + consoleMessage);
            }
            catch (ArgumentException argException)
            {
                // Output file prefix (-p) contains illegal characters.
                if (argException.Message.Contains("Illegal characters in path"))
                    System.Console.Error.WriteLine(Environment.NewLine + argException.Message);
            }
            catch (UnauthorizedAccessException)
            {
                System.Console.Error.WriteLine(Environment.NewLine + "Access denied.");
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Console.Error.WriteLine(Environment.NewLine + "File not found.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.Error.WriteLine(Environment.NewLine + "Directory not found.");
            }
            catch (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;
                }
            }
        }