Skip to content

Commit 8cb4601

Browse files
committed
split Program class
1 parent 0b690d5 commit 8cb4601

File tree

4 files changed

+185
-168
lines changed

4 files changed

+185
-168
lines changed

PdfScribe/App.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<value>True</value>
3737
</setting>
3838
<setting name="StripNoRedistill" serializeAs="String">
39+
<!-- This will slow things down because another pass will be made through the postscript file - set it to "False" unless needed -->
3940
<value>True</value>
4041
</setting>
4142
</PdfScribe.Properties.Settings>

PdfScribe/PdfScribe.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<Compile Include="GhostScript64.cs" />
103103
<Compile Include="NativeMethods.cs" />
104104
<Compile Include="Program.cs" />
105+
<Compile Include="Program_2.cs" />
105106
<Compile Include="Properties\AssemblyInfo.cs">
106107
<SubType>Code</SubType>
107108
</Compile>

PdfScribe/Program.cs

Lines changed: 1 addition & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace PdfScribe
1111
{
12-
public class Program
12+
public partial class Program
1313
{
1414

1515

@@ -150,172 +150,5 @@ static void Application_UnhandledException(object sender, UnhandledExceptionEven
150150
errorDialogInstructionUnexpectedError);
151151
}
152152

153-
static bool GetPdfOutputFilename(ref String outputFile)
154-
{
155-
bool filenameRetrieved = false;
156-
switch (Properties.Settings.Default.AskUserForOutputFilename)
157-
{
158-
case (true):
159-
using (SetOutputFilename dialogOwner = new SetOutputFilename())
160-
{
161-
dialogOwner.TopMost = true;
162-
dialogOwner.TopLevel = true;
163-
dialogOwner.Show(); // Form won't actually show - Application.Run() never called
164-
// but having a topmost/toplevel owner lets us bring the SaveFileDialog to the front
165-
dialogOwner.BringToFront();
166-
using (SaveFileDialog pdfFilenameDialog = new SaveFileDialog())
167-
{
168-
pdfFilenameDialog.AddExtension = true;
169-
pdfFilenameDialog.AutoUpgradeEnabled = true;
170-
pdfFilenameDialog.CheckPathExists = true;
171-
pdfFilenameDialog.Filter = "pdf files (*.pdf)|*.pdf";
172-
pdfFilenameDialog.ShowHelp = false;
173-
pdfFilenameDialog.Title = "PDF Scribe - Set output filename";
174-
pdfFilenameDialog.ValidateNames = true;
175-
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("REDMON_DOCNAME")))
176-
pdfFilenameDialog.FileName = Environment.GetEnvironmentVariable("REDMON_DOCNAME");
177-
if (pdfFilenameDialog.ShowDialog(dialogOwner) == DialogResult.OK)
178-
{
179-
outputFile = pdfFilenameDialog.FileName;
180-
filenameRetrieved = true;
181-
}
182-
}
183-
dialogOwner.Close();
184-
}
185-
break;
186-
default:
187-
try
188-
{
189-
outputFile = GetOutputFilename();
190-
// Test if we can write to the destination
191-
using (FileStream newOutputFile = File.Create(outputFile))
192-
{ }
193-
File.Delete(outputFile);
194-
filenameRetrieved = true;
195-
}
196-
catch (Exception ex) when (ex is ArgumentException ||
197-
ex is ArgumentNullException ||
198-
ex is NotSupportedException ||
199-
ex is DirectoryNotFoundException)
200-
{
201-
logEventSource.TraceEvent(TraceEventType.Error,
202-
(int)TraceEventType.Error,
203-
errorDialogOutputFilenameInvalid + Environment.NewLine +
204-
"Exception message: " + ex.Message);
205-
DisplayErrorMessage(errorDialogCaption,
206-
errorDialogOutputFilenameInvalid);
207-
}
208-
catch (PathTooLongException ex)
209-
{
210-
// filename is greater than 260 characters
211-
logEventSource.TraceEvent(TraceEventType.Error,
212-
(int)TraceEventType.Error,
213-
errorDialogOutputFilenameTooLong + Environment.NewLine +
214-
"Exception message: " + ex.Message);
215-
DisplayErrorMessage(errorDialogCaption,
216-
errorDialogOutputFilenameTooLong);
217-
}
218-
catch (UnauthorizedAccessException ex)
219-
{
220-
logEventSource.TraceEvent(TraceEventType.Error,
221-
(int)TraceEventType.Error,
222-
errorDialogOutputFileAccessDenied + Environment.NewLine +
223-
"Exception message: " + ex.Message);
224-
// Can't write to target dir
225-
DisplayErrorMessage(errorDialogCaption,
226-
errorDialogOutputFileAccessDenied);
227-
}
228-
break;
229-
}
230-
return filenameRetrieved;
231-
232-
}
233-
234-
private static String GetOutputFilename()
235-
{
236-
String outputFilename = Path.GetFullPath(Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile));
237-
// Check if there are any % characters -
238-
// even though it's a legal Windows filename character,
239-
// it is a special character to Ghostscript
240-
if (outputFilename.Contains("%"))
241-
throw new ArgumentException("OutputFile setting contains % character.");
242-
return outputFilename;
243-
}
244-
245-
246-
/// <summary>
247-
/// Opens the PDF in the default viewer
248-
/// if the OpenAfterCreating app setting is "True"
249-
/// and the file extension is .PDF
250-
/// </summary>
251-
/// <param name="pdfFilename"></param>
252-
static void DisplayPdf(String pdfFilename)
253-
{
254-
if (Properties.Settings.Default.OpenAfterCreating &&
255-
!String.IsNullOrEmpty(Path.GetExtension(pdfFilename)) &&
256-
(Path.GetExtension(pdfFilename).ToUpper() == ".PDF"))
257-
{
258-
Process.Start(pdfFilename);
259-
}
260-
}
261-
262-
/// <summary>
263-
/// Displays up a topmost, OK-only message box for the error message
264-
/// </summary>
265-
/// <param name="boxCaption">The box's caption</param>
266-
/// <param name="boxMessage">The box's message</param>
267-
static void DisplayErrorMessage(String boxCaption,
268-
String boxMessage)
269-
{
270-
271-
MessageBox.Show(boxMessage,
272-
boxCaption,
273-
MessageBoxButtons.OK,
274-
MessageBoxIcon.Error,
275-
MessageBoxDefaultButton.Button1,
276-
MessageBoxOptions.DefaultDesktopOnly);
277-
278-
}
279-
280-
static void StripNoDistill(String postscriptFile)
281-
{
282-
if (Properties.Settings.Default.StripNoRedistill)
283-
{
284-
String strippedFile = Path.GetTempFileName();
285-
286-
using (StreamReader inputReader = new StreamReader(File.OpenRead(postscriptFile), System.Text.Encoding.UTF8))
287-
using (StreamWriter strippedWriter = new StreamWriter(File.OpenWrite(strippedFile), new UTF8Encoding(false)))
288-
{
289-
bool redistillPhraseFound = false;
290-
String inputLine;
291-
while (!inputReader.EndOfStream)
292-
{
293-
inputLine = inputReader.ReadLine();
294-
if (inputLine != null)
295-
{
296-
if (redistillPhraseFound)
297-
{
298-
if (inputLine == "%ADOEndClientInjection: DocumentSetup Start \"No Re-Distill\"")
299-
redistillPhraseFound = false;
300-
}
301-
else
302-
{
303-
if (inputLine == "%ADOBeginClientInjection: DocumentSetup Start \"No Re-Distill\"")
304-
redistillPhraseFound = true;
305-
else
306-
strippedWriter.WriteLine(inputLine);
307-
308-
}
309-
}
310-
}
311-
strippedWriter.Close();
312-
inputReader.Close();
313-
}
314-
315-
File.Delete(postscriptFile);
316-
File.Move(strippedFile, postscriptFile);
317-
}
318-
319-
}
320153
}
321154
}

PdfScribe/Program_2.cs

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)