|
21 | 21 | using System.Collections;
|
22 | 22 | using System.Collections.Generic;
|
23 | 23 | using System.Diagnostics;
|
| 24 | +using System.Drawing.Printing; |
24 | 25 | using System.IO;
|
25 | 26 | using System.Reflection;
|
26 | 27 | using System.Security.AccessControl;
|
@@ -48,7 +49,8 @@ public enum ExitCode : int
|
48 | 49 | WorksheetNotFound = 256,
|
49 | 50 | EmptyWorksheet = 512,
|
50 | 51 | PDFProtectedDocument = 1024,
|
51 |
| - ApplicationError = 2048 |
| 52 | + ApplicationError = 2048, |
| 53 | + NoPrinters = 4096 |
52 | 54 | }
|
53 | 55 |
|
54 | 56 | public enum MergeMode : int
|
@@ -135,6 +137,14 @@ static void Main(string[] args)
|
135 | 137 | options["pdf_restrict_accessibility_extraction"] = false;
|
136 | 138 | options["pdf_restrict_full_quality"] = false;
|
137 | 139 |
|
| 140 | + // We need some printers to keep office happy |
| 141 | + Dictionary<string,bool> installedPrinters = GetInstalledPrinters(); |
| 142 | + if (installedPrinters.Count <= 0) |
| 143 | + { |
| 144 | + Console.WriteLine("There are no installed printers, so conversion can not proceed"); |
| 145 | + Environment.Exit((int)(ExitCode.Failed | ExitCode.NoPrinters)); |
| 146 | + } |
| 147 | + |
138 | 148 | // Strings used in error messages for different options
|
139 | 149 | var optionNameMap = new Dictionary<string, string>()
|
140 | 150 | {
|
@@ -391,11 +401,20 @@ static void Main(string[] args)
|
391 | 401 | case "printer":
|
392 | 402 | case "fallback_printer":
|
393 | 403 | // Only accept the next option if there are enough options
|
| 404 | + string optname = itemMatch.Groups[1].Value.ToLower(); |
394 | 405 | if (argIdx + 2 < args.Length)
|
395 | 406 | {
|
396 |
| - options[itemMatch.Groups[1].Value.ToLower()] = args[argIdx + 1]; |
| 407 | + options[optname] = args[argIdx + 1]; |
397 | 408 | argIdx++;
|
398 | 409 | }
|
| 410 | + if (optname.Equals("printer") || optname.Equals("fallback_printer")) |
| 411 | + { |
| 412 | + if (!installedPrinters.ContainsKey(((string)options[optname]).ToLowerInvariant())) { |
| 413 | + // The requested printer did not exists |
| 414 | + Console.WriteLine("The printer \"{0}\" is not installed", options[optname]); |
| 415 | + Environment.Exit((int)(ExitCode.Failed | ExitCode.InvalidArguments)); |
| 416 | + } |
| 417 | + } |
399 | 418 | break;
|
400 | 419 | case "screen":
|
401 | 420 | options["print"] = false;
|
@@ -988,6 +1007,16 @@ static PdfDocument ReadExistingPDFDocument(String filename, String generatedFile
|
988 | 1007 | return dstDoc;
|
989 | 1008 | }
|
990 | 1009 |
|
| 1010 | + private static Dictionary<string, bool> GetInstalledPrinters() |
| 1011 | + { |
| 1012 | + Dictionary<string, bool> printers = new Dictionary<string, bool>(); |
| 1013 | + foreach (string name in PrinterSettings.InstalledPrinters) |
| 1014 | + { |
| 1015 | + printers[name.ToLowerInvariant()] = true; |
| 1016 | + } |
| 1017 | + return printers; |
| 1018 | + } |
| 1019 | + |
991 | 1020 | static void ShowHelp()
|
992 | 1021 | {
|
993 | 1022 | Console.Write(@"Converts Office documents to PDF from the command line.
|
|
0 commit comments