|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using System.Windows.Forms; |
| 8 | + |
| 9 | +namespace PdfScribe |
| 10 | +{ |
| 11 | + public partial class Program |
| 12 | + { |
| 13 | + static bool GetPdfOutputFilename(ref String outputFile) |
| 14 | + { |
| 15 | + bool filenameRetrieved = false; |
| 16 | + switch (Properties.Settings.Default.AskUserForOutputFilename) |
| 17 | + { |
| 18 | + case (true): |
| 19 | + using (SetOutputFilename dialogOwner = new SetOutputFilename()) |
| 20 | + { |
| 21 | + dialogOwner.TopMost = true; |
| 22 | + dialogOwner.TopLevel = true; |
| 23 | + dialogOwner.Show(); // Form won't actually show - Application.Run() never called |
| 24 | + // but having a topmost/toplevel owner lets us bring the SaveFileDialog to the front |
| 25 | + dialogOwner.BringToFront(); |
| 26 | + using (SaveFileDialog pdfFilenameDialog = new SaveFileDialog()) |
| 27 | + { |
| 28 | + pdfFilenameDialog.AddExtension = true; |
| 29 | + pdfFilenameDialog.AutoUpgradeEnabled = true; |
| 30 | + pdfFilenameDialog.CheckPathExists = true; |
| 31 | + pdfFilenameDialog.Filter = "pdf files (*.pdf)|*.pdf"; |
| 32 | + pdfFilenameDialog.ShowHelp = false; |
| 33 | + pdfFilenameDialog.Title = "PDF Scribe - Set output filename"; |
| 34 | + pdfFilenameDialog.ValidateNames = true; |
| 35 | + if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("REDMON_DOCNAME"))) |
| 36 | + pdfFilenameDialog.FileName = Environment.GetEnvironmentVariable("REDMON_DOCNAME"); |
| 37 | + if (pdfFilenameDialog.ShowDialog(dialogOwner) == DialogResult.OK) |
| 38 | + { |
| 39 | + outputFile = pdfFilenameDialog.FileName; |
| 40 | + filenameRetrieved = true; |
| 41 | + } |
| 42 | + } |
| 43 | + dialogOwner.Close(); |
| 44 | + } |
| 45 | + break; |
| 46 | + default: |
| 47 | + try |
| 48 | + { |
| 49 | + outputFile = GetOutputFilename(); |
| 50 | + // Test if we can write to the destination |
| 51 | + using (FileStream newOutputFile = File.Create(outputFile)) |
| 52 | + { } |
| 53 | + File.Delete(outputFile); |
| 54 | + filenameRetrieved = true; |
| 55 | + } |
| 56 | + catch (Exception ex) when (ex is ArgumentException || |
| 57 | + ex is ArgumentNullException || |
| 58 | + ex is NotSupportedException || |
| 59 | + ex is DirectoryNotFoundException) |
| 60 | + { |
| 61 | + logEventSource.TraceEvent(TraceEventType.Error, |
| 62 | + (int)TraceEventType.Error, |
| 63 | + errorDialogOutputFilenameInvalid + Environment.NewLine + |
| 64 | + "Exception message: " + ex.Message); |
| 65 | + DisplayErrorMessage(errorDialogCaption, |
| 66 | + errorDialogOutputFilenameInvalid); |
| 67 | + } |
| 68 | + catch (PathTooLongException ex) |
| 69 | + { |
| 70 | + // filename is greater than 260 characters |
| 71 | + logEventSource.TraceEvent(TraceEventType.Error, |
| 72 | + (int)TraceEventType.Error, |
| 73 | + errorDialogOutputFilenameTooLong + Environment.NewLine + |
| 74 | + "Exception message: " + ex.Message); |
| 75 | + DisplayErrorMessage(errorDialogCaption, |
| 76 | + errorDialogOutputFilenameTooLong); |
| 77 | + } |
| 78 | + catch (UnauthorizedAccessException ex) |
| 79 | + { |
| 80 | + logEventSource.TraceEvent(TraceEventType.Error, |
| 81 | + (int)TraceEventType.Error, |
| 82 | + errorDialogOutputFileAccessDenied + Environment.NewLine + |
| 83 | + "Exception message: " + ex.Message); |
| 84 | + // Can't write to target dir |
| 85 | + DisplayErrorMessage(errorDialogCaption, |
| 86 | + errorDialogOutputFileAccessDenied); |
| 87 | + } |
| 88 | + break; |
| 89 | + } |
| 90 | + return filenameRetrieved; |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + private static String GetOutputFilename() |
| 95 | + { |
| 96 | + String outputFilename = Path.GetFullPath(Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile)); |
| 97 | + // Check if there are any % characters - |
| 98 | + // even though it's a legal Windows filename character, |
| 99 | + // it is a special character to Ghostscript |
| 100 | + if (outputFilename.Contains("%")) |
| 101 | + throw new ArgumentException("OutputFile setting contains % character."); |
| 102 | + return outputFilename; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Opens the PDF in the default viewer |
| 108 | + /// if the OpenAfterCreating app setting is "True" |
| 109 | + /// and the file extension is .PDF |
| 110 | + /// </summary> |
| 111 | + /// <param name="pdfFilename"></param> |
| 112 | + static void DisplayPdf(String pdfFilename) |
| 113 | + { |
| 114 | + if (Properties.Settings.Default.OpenAfterCreating && |
| 115 | + !String.IsNullOrEmpty(Path.GetExtension(pdfFilename)) && |
| 116 | + (Path.GetExtension(pdfFilename).ToUpper() == ".PDF")) |
| 117 | + { |
| 118 | + Process.Start(pdfFilename); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Displays up a topmost, OK-only message box for the error message |
| 124 | + /// </summary> |
| 125 | + /// <param name="boxCaption">The box's caption</param> |
| 126 | + /// <param name="boxMessage">The box's message</param> |
| 127 | + static void DisplayErrorMessage(String boxCaption, |
| 128 | + String boxMessage) |
| 129 | + { |
| 130 | + |
| 131 | + MessageBox.Show(boxMessage, |
| 132 | + boxCaption, |
| 133 | + MessageBoxButtons.OK, |
| 134 | + MessageBoxIcon.Error, |
| 135 | + MessageBoxDefaultButton.Button1, |
| 136 | + MessageBoxOptions.DefaultDesktopOnly); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + static void StripNoDistill(String postscriptFile) |
| 141 | + { |
| 142 | + if (Properties.Settings.Default.StripNoRedistill) |
| 143 | + { |
| 144 | + String strippedFile = Path.GetTempFileName(); |
| 145 | + |
| 146 | + using (StreamReader inputReader = new StreamReader(File.OpenRead(postscriptFile), System.Text.Encoding.UTF8)) |
| 147 | + using (StreamWriter strippedWriter = new StreamWriter(File.OpenWrite(strippedFile), new UTF8Encoding(false))) |
| 148 | + { |
| 149 | + bool redistillPhraseFound = false; |
| 150 | + String inputLine; |
| 151 | + while (!inputReader.EndOfStream) |
| 152 | + { |
| 153 | + inputLine = inputReader.ReadLine(); |
| 154 | + if (inputLine != null) |
| 155 | + { |
| 156 | + if (redistillPhraseFound) |
| 157 | + { |
| 158 | + if (inputLine == "%ADOEndClientInjection: DocumentSetup Start \"No Re-Distill\"") |
| 159 | + redistillPhraseFound = false; |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + if (inputLine == "%ADOBeginClientInjection: DocumentSetup Start \"No Re-Distill\"") |
| 164 | + redistillPhraseFound = true; |
| 165 | + else |
| 166 | + strippedWriter.WriteLine(inputLine); |
| 167 | + |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + strippedWriter.Close(); |
| 172 | + inputReader.Close(); |
| 173 | + } |
| 174 | + |
| 175 | + File.Delete(postscriptFile); |
| 176 | + File.Move(strippedFile, postscriptFile); |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | +} |
0 commit comments