コード例 #1
0
ファイル: Program.cs プロジェクト: stchan/PdfMiniTools
 private static bool ValidateOptions(Options commandLineOptions)
 {
     bool validatedOK = false;
     StringBuilder errorMessage = new StringBuilder();
     switch (commandLineOptions.Items.Count)
     {
         case 0:
             errorMessage.Append(messageNoInputFileSpecifed);
             break;
         case 1:
             if (!commandLineOptions.showAll &&
                 !commandLineOptions.showFields)
             {
                 commandLineOptions.showInfo = true;
             }
             validatedOK = true;
             break;
         default:
             validatedOK = true;
             break;
     }
     /*
     if (commandLineOptions.Items.Count > 0)
     {
     }
     /*
     else
     {
         errorMessage.Append(messageNoInputFileSpecifed);
     }
      */
     //System.Console.WriteLine(commandLineOptions.Items.Count.ToString() + " commandline options entered.");
     if (!validatedOK) System.Console.Error.WriteLine(errorMessage.ToString());
     return validatedOK;
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: stchan/PdfMiniTools
 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 infoTask = new TaskProcessor();
                 infoTask.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;
     }
 }
コード例 #3
0
ファイル: TaskProcessor.cs プロジェクト: stchan/PdfMiniTools
 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;
         }
     }
 }