Beispiel #1
0
 public void ProcessTask(Options commandLineOptions)
 {
     String inputFile = commandLineOptions.Items[0];
     var infoTools = new CoreTools();
     Dictionary<String, String> pdfInfo = new Dictionary<String, String>();
     try
     {
         if (commandLineOptions.showAll || commandLineOptions.showInfo)
         {
             foreach (KeyValuePair<String, String> pdfInfoPair in infoTools.RetrieveBasicProperties(inputFile))
             {
                 pdfInfo.Add(pdfInfoPair.Key, pdfInfoPair.Value);
             }
             foreach (KeyValuePair<String, String> pdfInfoPair in infoTools.RetrieveInfo(inputFile))
             {
                 pdfInfo.Add(pdfInfoPair.Key, pdfInfoPair.Value);
             }
         }
         if (commandLineOptions.showAll || commandLineOptions.showFields)
         {
             foreach (KeyValuePair<String, String> pdfInfoPair in infoTools.RetrieveAcroFieldsData(inputFile))
             {
                 pdfInfo.Add(pdfInfoPair.Key, pdfInfoPair.Value);
             }
         }
         WriteResults(commandLineOptions.csvOutput, pdfInfo);
     }
     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 (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;
         }
     }
 }
Beispiel #2
0
 public void ProcessTask(Options commandLineOptions)
 {
     var splitTools = new CoreTools();
     try
     {
         splitTools.EvenOddMerge(commandLineOptions.Items[0],
                                 commandLineOptions.Items[1],
                                 commandLineOptions.Items[2],
                                 commandLineOptions.skipExtraPages);
     }
     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
         {
             // Some other IOException we weren't expecting
             throw;
         }
     }
 }
Beispiel #3
0
 public void ProcessTask(Options commandLineOptions)
 {
     var pdfTools = new CoreTools();
     List<String> inputFiles = new List<string>(commandLineOptions.Items);
     String outputFile = inputFiles[inputFiles.Count - 1];
     inputFiles.RemoveAt(inputFiles.Count - 1);
     try
     {
         pdfTools.ConcatenatePDFFiles(inputFiles.ToArray(), outputFile);
     }
     catch (UnauthorizedAccessException)
     {
         System.Console.Error.WriteLine("Access denied.");
     }
     catch (System.IO.FileNotFoundException)
     {
         System.Console.Error.WriteLine("File not found.");
     }
     catch (System.IO.DirectoryNotFoundException)
     {
         System.Console.Error.WriteLine("Directory not found.");
     }
 }
Beispiel #4
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;
                }
            }
        }
Beispiel #5
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.");
            }
        }