-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
177 lines (150 loc) · 6.27 KB
/
Copy pathProgram.cs
File metadata and controls
177 lines (150 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Diagnostics;
using CommandLine;
public class Options
{
[Option("printer", Required = false, HelpText = "Name of the printer.")]
public string PrinterName { get; set; }
[Option("watchpath", Required = true, HelpText = "Directory to watch for changes.")]
public string WatchPath { get; set; }
[Option("printedpath", Required = false, HelpText = "Where to place the files when printed.")]
public string PrintedPath { get; set; }
}
class Program
{
private string printer = null;
private static string watchDirectory = @"D:\share\TO-PRINT";
private static string printedDirectory = @"D:\share\TO-PRINT\PRINTED";
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(options =>
{
printedDirectory = options.PrintedPath;
Console.WriteLine("Printer: " + options.PrinterName);
Console.WriteLine("Watch Path: " + options.WatchPath);
Console.WriteLine("Printed Path: " + options.PrintedPath);
// Ensure directories exist.
Directory.CreateDirectory(options.WatchPath);
Directory.CreateDirectory(options.PrintedPath);
using (var watcher = new FileSystemWatcher(options.WatchPath, "*.pdf"))
{
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
watcher.Created += OnFileCreated;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Monitoring directory: " + options.WatchPath);
Console.WriteLine("Press 'q' then ENTER to quit.");
while (Console.ReadLine()?.ToLower() != "q") { }
}
})
.WithNotParsed(errors =>
{
// Handle errors, display help, etc.
Console.WriteLine("Invalid arguments provided.");
Environment.Exit(1);
});
}
private static void OnFileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("New file detected: " + e.FullPath);
// Wait briefly to ensure the file is completely written.
Thread.Sleep(1000);
bool printed = false;
string extension = Path.GetExtension(e.FullPath);
if (extension.Equals(".pdf", StringComparison.OrdinalIgnoreCase))
{
try
{
PrintPdfViaAcrobat(e.FullPath);
//PrintPdf(e.FullPath);
Console.WriteLine("PDF printed successfully using gembox.");
printed = true;
}
catch (Exception ex)
{
Console.WriteLine("Error printing PDF file: " + ex.Message);
}
}
// Code to handle non PDF files, might fix later but right now it's a little buggy
//else
//{
// try
// {
// Process printProcess = new Process();
// printProcess.StartInfo.FileName = e.FullPath;
// printProcess.StartInfo.Verb = "Print";
// printProcess.StartInfo.UseShellExecute = true; // Needed for non-executable files.
// printProcess.StartInfo.CreateNoWindow = true;
// printProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// printProcess.Start();
// printProcess.WaitForExit(10000); // Wait up to 10 seconds.
// Console.WriteLine("Print command executed.");
// printed = true;
// }
// catch (Exception ex)
// {
// Console.WriteLine("Error printing file: " + ex.Message);
// }
//}
if (printed)
{
try
{
string destinationPath = Path.Combine(printedDirectory, Path.GetFileName(e.FullPath));
if (File.Exists(destinationPath))
File.Delete(destinationPath);
File.Move(e.FullPath, destinationPath);
Console.WriteLine("Moved file to: " + destinationPath);
}
catch (Exception ex)
{
Console.WriteLine("Error moving file: " + ex.Message);
}
}
}
public static void PrintPdfViaAcrobat(string pdfFilePath, string acrobatReaderPath = @"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe", string printerName = null)
{
// Validate the PDF file path.
if (string.IsNullOrWhiteSpace(pdfFilePath))
throw new ArgumentException("PDF file path cannot be null or empty.", nameof(pdfFilePath));
if (!File.Exists(pdfFilePath))
throw new FileNotFoundException("The specified PDF file was not found.", pdfFilePath);
// Validate the Acrobat Reader executable path.
if (!File.Exists(acrobatReaderPath))
throw new FileNotFoundException("Acrobat Reader executable was not found.", acrobatReaderPath);
// Prepare the command-line arguments.
// Acrobat Reader’s /t switch prints the file. The syntax is:
// AcroRd32.exe /t <PDFfile> [<printerName> [<driverName> [<portName>]]]
// For simplicity, if a printer name is provided we pass it; otherwise, only the PDF file.
string arguments = string.IsNullOrEmpty(printerName)
? $"/t \"{pdfFilePath}\""
: $"/t \"{pdfFilePath}\" \"{printerName}\"";
// Setup the process to start Acrobat Reader with these arguments.
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = acrobatReaderPath,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
try
{
using (Process process = Process.Start(psi))
{
if (process != null)
{
// Wait for a reasonable time for Acrobat Reader to start printing.
process.WaitForExit(10000); // wait up to 10 seconds
if (!process.HasExited)
{
process.Kill();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error printing PDF file: {ex.Message}");
}
}
}